vba在另一个工作簿的列中search值?

我正在使用下面的代码来尝试find另一个工作簿F列中的值。

Sub WorkBookCheck() 'On Error Resume Next 'Extract Aduit Percents Dim sFound As String Dim FindRow As Range Dim WB2 As Workbook Dim app As New Excel.Application sFound = Dir(Worksheets("assets").Range("A12").Value & "\" & "*_UK_results.xl*") 'the first one found If sFound <> "" Then app.Visible = False 'Visible is False by default, so this isn't necessary Set WB2 = Workbooks.Open(Filename:=Worksheets("assets").Range("A12").Value & "\" & sFound, ReadOnly:=True) End If With WB2 With Worksheets("Result sheets Lidl") Set FindRow = .Range("F:F").Find(What:="Chapter result", LookIn:=xlValues) MsgBox FindRow If Not FindRow Is Nothing Then MsgBox FindRow End If End With End With End Sub 

我收到以下错误:

 Object Variable or With Block Variable not set 

在这一行上:

 MsgBox FindRow 

请有人告诉我我要去哪里错了?

此外,这个值在列中出现多次,所以我想能够显示每个匹配值的消息find。 有人也可以告诉我如何循环这个? 我是全新的vba。 谢谢

1-你的两个嵌套块没有意义。 你只需要一个:

当然,如果结果是Nothing你不能msgbox它。

 With WB2.Worksheets("Result sheets Lidl") Set FindRow = .Range("F:F").Find(What:="Chapter result", LookIn:=xlValues, LookAt:=xlPart, MatchCase:=False) If Not FindRow Is Nothing Then MsgBox FindRow.Address End With 

请注意.Find有很多其他的参数,如果你不在调用中设置它们,它们将inheritance它们从最后一次search的值,无论是在GUI还是在VBA中。 通常build议设置所有的,或者使用Match替代。