1
下面的代碼將找到相鄰的單詞,但如果單詞不在電子表格中,我嘗試添加錯誤處理程序。我收到錯誤對象變量或未設置塊變量。問題是什麼?或者您可以幫助修復錯誤信息,以便如果未找到該單詞,則msgbox將顯示MsgBox「對不起,未找到文本,請重試,宏停止」。謝謝!VBA Excel錯誤對象變量或塊變量未設置
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
你所需要的',如果不匹配是前'findOffset = match.Offset nothing'線(1 ).Value'行 – JosieP
@JosieP是正確的。當'match Is Nothing'時你的錯誤結果。 –
是的,我試過,但仍然給出了錯誤 – mgrobins