尝试在excel中自动执行冗长的数据输入过程。
由于某种原因,我第二次尝试在给定范围内找到一个值,我得到一个运行时91错误。我无法弄清楚为什么会这样。如果我将范围从“AF3:AF30”更改为第一次设置范围(“B2:AD2”),程序将继续通过该行没有问题。但由于某种原因,除了第一个“B2:AD2”之外的任何范围都会给出运行时间!
码:
Sub AutoFill()
Range("B8:AD57").Select
Selection.ClearContents
Range("B8").Select
Dim Rng As Range, cell As Range
Set Rng = Worksheets("Avon Trailer List").Range("E4:E38")
For Each cell In Rng
Dim temp As String
temp = Replace(cell.Value(), " ", "")
temp = Replace(temp, "+", "")
If temp = "" Then
Exit For
Else
col = ""
tempa = SplitMultiDelims(temp, "/,\)(")
For i = 0 To UBound(tempa)
If InStr(tempa(i), "A") = 1 Then
strInput = Worksheets("Pallet Check").Range("B2:AD2").Find(tempa(i)).Address()
lnRow = Range(strInput).Row
strCol = Left(strInput, Len(strInput) - Len(CStr(lnRow)))
strCol = Replace(strCol, "S", "")
Search = tempa(i)
ElseIf InStr(tempa(i), "A") > 1 Or InStr(tempa(i), "B") > 1 Or InStr(tempa(i), "a") > 1 Or InStr(tempa(i), "b") > 1 Then
strInput = Worksheets("Pallet Check").Range("AF3:AF30").Find(Search).Address()
lnRow = Range(strInput).Row
Range("AH" & lnRow).Value() = "(" & tempa(i) & ")"
ElseIf tempa(i) <> "" Then
Worksheets("Pallet Check").Range(strCol & tempa(i) + 7).Value() = Worksheets("Pallet Check").Range(strCol & tempa(i) + 7).Value() & tempa(i)
End If
Next i
End If
Next cell
End Sub
运行时错误91发生在以下代码行:
strInput = Worksheets("Pallet Check").Range("AF3:AF30").Find(Search).Address()
Find()。如果没有找到异常(代码91)的地址中断。这是因为.Find()返回一个实际指向一个特定单元格的范围,或者在找不到时返回Nothing,并且'Nothing'。地址没有意义(n.b:VBA中对象的空值为'Nothing')。所以我会替换那条线
Fnd = Worksheets("Pallet Check").Range("AF3:AF30").Find(Search)
if Not Fnd Is Nothing then
strInput = Fnd.Address
...