下标超出范围的VBA变体数据

我对VBA有点新,经过一番研究,我把一些工作代码拼凑在一起(见底)。 根据这些代码和其他一些东西,我发现我创build了这个混乱:

Sub TranslateNewBOM() Dim NewFootPrint As Variant Dim Translated As Variant Dim temp As String Dim n As Long Dim MaxRow As Integer Do MaxRow = n n = n + 1 Loop Until Cells(n, 3).Value = "stop" Cells(3, 8).EntireColumn.Insert NewFootPrint = Range(Cells(3, 7), Cells(MaxRow, 7)).CurrentRegion.Value Translated = Range(Cells(3, 8), Cells(MaxRow, 8)).CurrentRegion.Value For i = 3 To MaxRow temp = NewFootPrint(i, 7) 'THIS IS THE LINE THAT GIVES ME THE ERROR temp = Left(temp, 3) If temp = "" Then Cells(i, 5).Value = "void" End If If temp = "CAP" Then Translated(i, 8).Value = "SMC" & Right(NewFootPrint(i, 7).Text, _ Len(NewFootPrint(i, 7).Text) - 3) Translated(i, 8) = Replace(Translated(i, 8).Text, " ", "-") End If Next i End Sub 

这个错误对我没有意义。 可能是因为我不完全理解VBA中的变体或数组。 但是,当我将它与其他代码进行比较时,我发现语法与NewFootPrint相近。 唯一的区别是所涉及的数字要大得多。 这里的工作代码,由于其可怕的命名约定,这是一个正在进行的工作。 NewFootPrint〜= DataRangeNew在我心中。

编辑:底部代码不起作用了。 我不记得改变任何东西,但现在popup同样超出范围的错误。 当我们说话时,我正在失去头发。

 Sub GetandSortBOM() ' Data is imported through a template from a raw database. ' Variable declarations Dim xnum As Integer Dim MaxRows As Long Dim Rng As Variant Dim DataRangeNew As Variant Dim DataRangeOld As Variant Dim DataRangeNewFoot As Variant Dim DataRangeNewTo As Variant Dim DataRangeNewFootTo As Variant Dim Irow As Long Dim rows As Long Dim MaxCols As Long Dim MyVarOld As String Dim MyVarNew As String Dim temp() As String DataRangeNew = Range(Cells(2, 12), Cells(1587, 12)).CurrentRegion.Value ' These work together DataRangeNewFoot = Range(Cells(2, 13), Cells(1587, 13)).CurrentRegion.Value ' and store data in DataRangeOld = Range(Cells(3, 3), Cells(MaxRows, 3)).CurrentRegion.Value ' columns without cell DataRangeNewTo = Range(Cells(3, 8), Cells(MaxRows, 8)).CurrentRegion.Value ' manipulation. Too much DataRangeNewFootTo = Range(Cells(3, 7), Cells(MaxRows, 7)).CurrentRegion.Value ' data to go through without Rng = Range(Cells(3, 7), Cells(MaxRows, 8)).CurrentRegion.Value NumRows = Range(Cells(2, 12), Cells(1587, 12)).CurrentRegion.rows.Count For rows = 3 To MaxRows MyVarOld = DataRangeOld(rows, 3) For Irow = 1 To NumRows '''''''''''''''''''''''''''''''''''''''''''''' MyVarNew = DataRangeNew(Irow, 12)' Why does this work, but not my other code? ' '''''''''''''''''''''''''''''''''''''''''''''' If MyVarOld = MyVarNew Then DataRangeNewTo(rows, 8) = DataRangeOld(rows, 3) DataRangeNewFootTo(rows, 7) = DataRangeNewFoot(Irow, 13) End If Next Irow Next rows ' Combines 2 columns of new data into a 2D array ReDim temp(1 To MaxRows, 1 To 2) ' Puts the information into the 2D array For i = 3 To MaxRows Rng(i, 7) = DataRangeNewFootTo(i, 7) Rng(i, 8) = DataRangeNewTo(i, 8) Next i ' Puts 2D array in cells Range(Cells(3, 7), Cells(MaxRows, 7)).CurrentRegion = Rng End Sub 

错误是运行时错误“9”:脚本超出范围。 对我来说,它似乎实际上在范围内; 至less当你比较我的旧代码。 帮帮我?

在第一种情况下:

 NewFootPrint = Range(Cells(3, 7), Cells(MaxRow, 7)).CurrentRegion.Value 

不会创build一个包含7列的数组,因为它是单列。 :-)所以,试图索引它像NewFootPrint(i, 7)将无法正常工作。 你应该像这样初始化你的Variant

 NewFootPrint = Range(Cells(3, 7), Cells(MaxRow, 7)).Value 

或者,甚至更好:

 With Worksheets("whatever worksheet use") ' ... NewFootPrint = .Range(.Cells(3, 7), .Cells(MaxRow, 7)).Value ' ... End With 

并像这样访问它:

 NewFootPrint(i, 1) 

稍后编辑:获取范围从(r1,c1)到(r2,c2)的数据,其中r2≥ r1和c2≥ c1,将创build一个数组(1到r2-r1 + 1,1到c2-c1 + 1)。 所以你应该检查所有的循环和所有的索引。

  • 由于使用CurrentRegion ,NewFootPrint和Translated可能包含相同的值。 这是你的意图吗? 否则,删除CurrentRegion
  • 你确定这个数组有7列吗?
  • For rows = 3 To MaxRows应该可能是For rows = 0 To MaxRows - 4
    因为这样的数组是基于零的。
  • 您也可以Dim NewFootPrint as Range ,然后
    Set NewFootPrint = Range(Cells(3, 7), Cells(MaxRow, 7))