使用查找功能找不到任何内容-为什么不继续执行代码?

时间:2019-04-04 10:14:04

标签: excel vba

我是新来的,我不知道自己在做什么。我对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

1 个答案:

答案 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读起来很有趣。