String 下标超出范围的VBA变量数据

String 下标超出范围的VBA变量数据,string,excel,vba,range,variant,String,Excel,Vba,Range,Variant,我对excel VBA有点陌生,经过一些研究,我拼凑了一些工作代码(见下图)。基于这些代码和我发现的其他一些东西,我制造了这样的混乱: 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

我对excel VBA有点陌生,经过一些研究,我拼凑了一些工作代码(见下图)。基于这些代码和我发现的其他一些东西,我制造了这样的混乱:

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~=DataRange在我心目中是新的

编辑:底部代码不再工作。我不记得改变了什么,但现在又出现了同样的超出范围的错误。我们说话的时候我头发都掉光了

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”:子脚本超出范围。 在我看来,它实际上在这个范围内;至少和我以前的代码相比。 帮助?

在第一种情况下:

NewFootPrint = Range(Cells(3, 7), Cells(MaxRow, 7)).CurrentRegion.Value
不会创建包含7列的数组,因为它是单列。:-)因此,试图像
NewFootPrint(i,7)
那样对其进行索引是行不通的。您应该像这样初始化您的
变体

NewFootPrint = Range(Cells(3, 7), Cells(MaxRow, 7)).Value
NewFootPrint(i, 1)
或者,更好的是:

With Worksheets("whatever worksheet use")
    ' ...
    NewFootPrint = .Range(.Cells(3, 7), .Cells(MaxRow, 7)).Value
    ' ...
End With
然后像这样访问它:

NewFootPrint = Range(Cells(3, 7), Cells(MaxRow, 7)).Value
NewFootPrint(i, 1)
以后编辑: 获取(r1,c1)到(r2,c2)范围内的数据,其中r2≥r1和c2≥c1将创建一个数组(1到r2-r1+1,1到c2-c1+1)。因此,您应该检查所有for循环和所有索引。

  • 由于使用的是
    CurrentRegion
    ,因此NewFootPrint和Translated可能包含相同的值。这是你的意图吗?否则,请删除该
    CurrentRegion
  • 您确定数组有7列吗
  • 另外,行=3到MaxRows的
    可能应该是行=0到MaxRows-4的

    因为这样的数组是基于零的
  • 您还可以
    将NewFootPrint设置为范围
    ,然后
    Set NewFootPrint=Range(单元格(3,7)、单元格(MaxRow,7))

您是否检查了MaxRow的值?请在错误行之前尝试debug.print/msgbox,因为您使用的CurrentRegion、NewFootPrint和Translated可能包含相同的值。这是你的意图吗?您确定数组有7列吗?MaxRow的值是正确的,但在调试它时,第一次出现错误。所以我或7导致了错误。我一定是误解了我是如何声明我的变体的。我希望它在一列中包含数据。例如:NewFootPrint应该包含从Row3Col7到Row(MaxRows)col7的单元格谢谢,我发现这很有用。只要我得到代表,我就投票。