VBA Excel错误对象variables或未设置块variables

下面的代码将find相邻的单词,但我试图添加一个error handling程序,如果一个单词不在电子表格中。 我得到错误对象variables或块variables未设置。 问题是什么? 或者你可以帮助修复错误信息,以便如果没有find该单词msgbox显示MsgBox“对不起,文本没有find,请再试一次,macros停止”。 谢谢!

Sub Module6() ' 'FindPlusOffset&Count ' ' Dim ws As Worksheet Dim match As Range Dim findMe As String Dim findOffset As String Dim Number As Long Set ws = ThisWorkbook.Sheets("Sheet1") findMe = "report" 'word not in spreadsheet Set match = ws.Cells.Find(findMe) findOffset = match.Offset(, 1).Value 'error occurs object variable or with block variable not set on this line Number = Evaluate("=SUMPRODUCT(--(NOT(ISERROR(FIND(""" & findMe & """,A1:AZ96,1)))))") If (Not match Is Nothing) Then MsgBox "The adjacent word to """ & findMe & """ is """ & findOffset & """. It is found "" " & Number & """ times!" Else 'not match MsgBox "Sorry the text was not found please try again. Macro stopping" End If End Sub 

正如JosieP所说,这段代码按预期工作(我刚刚尝试过)

  Sub Module6() ' 'FindPlusOffset&Count ' ' Dim ws As Worksheet Dim match As Range Dim findMe As String Dim findOffset As String Dim Number As Long Set ws = ThisWorkbook.Sheets("Sheet1") findMe = "report" 'word not in spreadsheet Set match = ws.Cells.Find(findMe) If (Not match Is Nothing) Then findOffset = match.Offset(, 1).Value 'error occurs object variable or with block variable not set on this line Number = Evaluate("=SUMPRODUCT(--(NOT(ISERROR(FIND(""" & findMe & """,A1:AZ96,1)))))") MsgBox "The adjacent word to """ & findMe & """ is """ & findOffset & """. It is found "" " & Number & """ times!" Else 'not match MsgBox "Sorry the text was not found please try again. Macro stopping" End If End Sub