我是新来的,我不知道自己在做什么。我对find函数有问题-如果找不到任何内容,则会引发错误“对象变量或未设置块变量”,我尝试过if循环,但调试器不会考虑它们。它压在findFeature = ThisWorkbook.Sheets("featureListLTE").Range("A1:A1000").Find(featureID, LookIn:=xlValues, lookat:=xlWhole)
行上
Dim newInput As Long
newInput = ThisWorkbook.Sheets("input").Range("A1", ThisWorkbook.Sheets("input").Range("A1").End(xlDown)).Rows.Count
Dim i As Integer
For i = 1 To newInput
'find feature on feature list
Dim featureID As String
featureID = ThisWorkbook.Sheets("input").Cells(i, 1).Value
MsgBox featureID
Dim findFeature As Variant
Dim findFeatureRow As Integer
findFeature = ThisWorkbook.Sheets("featureListLTE").Range("A1:A1000").Find(featureID, LookIn:=xlValues, lookat:=xlWhole)
findFeatureRow = ThisWorkbook.Sheets("featureListLTE").Range("A1:A1000").Find(featureID, LookIn:=xlValues, lookat:=xlWhole).Row
If findFeature.Value Is Nothing Then
MsgBox "nima"
Else: MsgBox findFeatureRow
End If
Next i
答案 0 :(得分:0)
如果找不到范围,则不能有.Value
或.Row
。检查是否找到范围后,将它们放入块中。例如( unested ),您的代码可以写为
Dim findFeature As Range
Set findFeature = ThisWorkbook.Sheets("featureListLTE").Range("A1:A1000").Find(featureID, LookIn:=xlValues, lookat:=xlWhole)
If Not findFeature Is Nothing Then
findFeatureRow = findFeature.Row
MsgBox findFeatureRow
Else
MsgBox "nima"
End If
您可能会发现.Find and .FindNext读起来很有趣。