错误9下标超出范围

错误9下标超出范围

本文介绍了Excel 2007 vba宏:错误9下标超出范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码,由@ Jon49为我编写,我适应我的目的:
我有这个代码的问题是,它适用于我的一些文件,但产生一个错误9下标范围在别人身上基本上我有一个循环,打开文件夹中的每个文件,然后执行foloowing代码。保存它,然后关闭它,然后再打开另一个。所有文件的格式是一样的。那么什么是产生错误?



当错误发生,我调试它往往显示原因有些在这里:

  sData(j,7)= vData(1,j + 10)
sData(j,8)= vData(i,j + b $ b sData(j,9)= vData(3,j + 10)
sData(j,10)= vData(2,j + 10)
pre>

SCROLL to BOTTOM我认为是导致错误



这里是代码:

  Range(k1)。选择
Dim参数As String
参数=范围(ActiveCell.End(xlToRight).Offset(0,0),ActiveCell).Count
Dim i As Long,j As Long,k As Long
Dim rData As Range
Dim sData ()As String,sName As String
Dim wks As Worksheet
Dim vData As Variant
Application.EnableEvents = False'初始化工作表
设置wks = ActiveSheet获取数据
设置rData = wks.UsedRange
vData = rData
ReDim sData(1到参数,1到rData.Columns.Count - 10)
rData。偏移(1).Clear
rData.Offset(11).Resize(1).Clear

< ---- ???不确定上面这一行的重要性,因为我注释了,代码仍然可以工作。

  For i = 1 To UBound(vData )
对于j = 1到UBound(sData)
对于k = 1到6
sData(j,k)= vData(i,k)
下一个k
sData(j,7)= vData(1,j + 10)
sData(j,8)= vData(i,j + 10)
sData(j,9)= vData j + 10)
sData(j,10)= vData(2,j + 10)
下一个j'打印转置数据
wks.Range(A& Application.Rows。计数).End(xlUp)_
.Offset(1).Resize(UBound(sData),UBound(sData,2))= sData
Next i
Application.EnableEvents = True






 范围K1)。选择
Range(ActiveCell.End(xlToRight).Offset(0,0),ActiveCell).Delete
Rows(2:& Parameters + 1).Delete





我注意到的一件事是在
上出现错误的文件有很少的参数(两个文件ive注意到它到目前为止有9个[将调用sData(j,10)= vData(2,j + 10)]和7 [sData(j,8)= vData(i,j + 10)]参数)每个都有10多个参数。参数为CH4,NO,NO2等

解决方案

您正在尝试访问不存在的值在您的sData或vData数组中。其中一个或者两个都比你想象的更少的行或列。尝试添加:

  MsgboxsData:#rows =& ubound(sData,1)& #cols =& _ 
ubound(sData,2)& vbcrlf&
vData#rows =& ubound(vData,1)& #cols =& ubound(vData,2)

紧随

  ReDim sData(1到参数,1到rData.Columns.Count  -  10)

看看你有什么价值。


I have a code, written for me by @Jon49 that i adapted to suit my purposes :The problem i have with this code is that it works for some of my files but produce an error 9 subscript out of range on others. essentially i have a loop that opens every file in a folder in turn performs the foloowing code. saves it then closes it before opening another one. The format of all the files is the same. so what is producing the error??

When the error happens and i debug it it tends to show the cause some where here :

sData(j, 7) = vData(1, j + 10)
sData(j, 8) = vData(i, j + 10)
sData(j, 9) = vData(3, j + 10)
sData(j, 10) = vData(2, j + 10)

SCROLL to BOTTOM for what i think is causing the error

Here is the code:

Range("k1").Select
Dim Parameters As String
Parameters = Range(ActiveCell.End(xlToRight).Offset(0, 0), ActiveCell).Count
Dim i As Long, j As Long, k As Long
Dim rData As Range
Dim sData() As String, sName As String
Dim wks As Worksheet
Dim vData As Variant
Application.EnableEvents = False     'Initialize worksheets
Set wks = ActiveSheet      'Get data
Set rData = wks.UsedRange
vData = rData
ReDim sData(1 To Parameters, 1 To rData.Columns.Count - 10)
rData.Offset(1).Clear
rData.Offset(11).Resize(1).Clear

<---- ??? not sure about importance of the line above because i commented out and the code will still work

For i = 1 To UBound(vData)
  For j = 1 To UBound(sData)
    For k = 1 To 6
      sData(j, k) = vData(i, k)
    Next k
    sData(j, 7) = vData(1, j + 10)
    sData(j, 8) = vData(i, j + 10)
    sData(j, 9) = vData(3, j + 10)
    sData(j, 10) = vData(2, j + 10)
  Next j         'Print transposed data
  wks.Range("A" & Application.Rows.Count).End(xlUp) _
  .Offset(1).Resize(UBound(sData), UBound(sData, 2)) = sData
Next i
Application.EnableEvents = True


Range("K1").Select
Range(ActiveCell.End(xlToRight).Offset(0, 0), ActiveCell).Delete
Rows("2:" & Parameters + 1).Delete

One thing that i noticed is the files that the error seems to be produced onhas few parameters ( the two files ive noticed it erroring so far have 9 [will debug to sData(j, 10) = vData(2, j + 10)] and 7 [sData(j, 8) = vData(i, j + 10)]parameters each ) the others it is okay with so far each have more than 10 parameters . parameters being CH4, NO, NO2 etc

解决方案

You're trying to access a value which doesn't exist in your sData or vData array. One or both of them have fewer "rows" or "columns" than you think. Try adding:

Msgbox "sData: #rows=" & ubound(sData,1) & "   #cols=" & _
        ubound(sData,2) & vbcrlf &
       "vData #rows=" & ubound(vData,1) &  "   #cols=" & ubound(vData,2)

Immediately after

ReDim sData(1 To Parameters, 1 To rData.Columns.Count - 10)

See what values you get.

这篇关于Excel 2007 vba宏:错误9下标超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 08:13