我对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中的Variants或Arrays。但是,当我将其与其他代码进行比较时,我的语法与NewFootPrint几乎相同。唯一的区别是所涉及的数字要大得多。这是工作代码,由于其可怕的命名约定,它正在进行中。 NewFootPrint~ = DataRangeNew在我的脑海里。
编辑:底部代码不再起作用了。我不记得改变任何东西,但现在出现同样超出范围的错误。我们说话的时候我会脱发。
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':Subcript超出范围。 对我来说,它似乎实际上在范围内;至少当你与我的旧代码比较时。 帮助
答案 0 :(得分:1)
在第一种情况下:
NewFootPrint = Range(Cells(3, 7), Cells(MaxRow, 7)).CurrentRegion.Value
将不创建一个包含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,将创建一个数组(1到r2-r1 + 1,1到c2-c1 + 1)。所以你应该检查所有你的for循环和所有你的索引。
答案 1 :(得分:0)
CurrentRegion
,因此NewFootPrint和Translated可能包含相同的值。这是你的意图吗?否则,请删除CurrentRegion
For rows = 3 To MaxRows
应该是For rows = 0 To MaxRows - 4
Dim NewFootPrint as Range
然后再
Set NewFootPrint = Range(Cells(3, 7), Cells(MaxRow, 7))