简体   繁体   English

使用find函数找不到任何内容 - 为什么它不继续使用代码?

[英]Nothing found using find function - why it doesn't proceed with the code?

I am new here and I don't know exactly what I am doing. 我是新来的,我不知道我在做什么。 I have a problem with a find function - if there is nothing found it throws the error 'Object variable or With block variable not set' I've tried if loop, but the debuger doesn't consider them. 我有一个查找函数的问题 - 如果没有找到它会抛出错误'对象变量或没有设置块变量'我尝试过循环,但debuger不考虑它们。 It crushes on findFeature = ThisWorkbook.Sheets("featureListLTE").Range("A1:A1000").Find(featureID, LookIn:=xlValues, lookat:=xlWhole) line 它粉碎了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

You can't have a .Value or .Row if no range was found. 如果没有找到范围,则不能有.Value.Row Put them inside a block after checking whether a range was found or not. 在检查是否找到范围后将它们放入块中。 For example ( untested ), your code can be written as 例如( 未经测试 ),您的代码可以写成

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

You may find .Find and .FindNext interesting to read. 你可能会发现.Find和.FindNext很有意思。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM