我正在評估這一行是否有錯誤(當它沒有找到匹配項時),然后在沒有找到匹配項時獲取我需要的資訊。當我在單步執行代碼時將滑鼠懸停在“= True”陳述句上時,它回傳一個值 true,但它只是拋出錯誤 1004,而不是轉到“If true”系列輸入框。
If IsError(Target.Offset(0, 1) = Application.WorksheetFunction.XLookup(WorksheetFunction.Text(Target, "General"), Sheets("tblItem").Columns(2), Sheets("tblItem").Columns(3))) = True Then
Target.Offset(0, 1) = InputBox("Description?")
Target.Offset(0, 2) = InputBox("Project Number?")
Target.Offset(0, 3) = InputBox("Brand?")
Target.Offset(0, 4) = InputBox("Brand Item Number?")
Target.Offset(0, 5) = InputBox("Unit Cost?")
Target.Offset(0, 6) = InputBox("Unit Weight?")
Target.Offset(0, 7) = InputBox("Weight's UoM?")
Else
Target.Offset(0, 1) = Application.WorksheetFunction.XLookup(WorksheetFunction.Text(Target, "General"), Sheets("tblItem").Columns(2), Sheets("tblItem").Columns(3))
End If
uj5u.com熱心網友回復:
您不能IsError()用來捕獲這樣的運行時錯誤:
Dim res
'no `WorksheetFunction`
res = Application.XLookup(Target.Value, Sheets("tblItem").Columns(2), _
Sheets("tblItem").Columns(3))
If IsError(res) Then
Target.Offset(0, 1) = InputBox("Description?")
Target.Offset(0, 2) = InputBox("Project Number?")
Target.Offset(0, 3) = InputBox("Brand?")
Target.Offset(0, 4) = InputBox("Brand Item Number?")
Target.Offset(0, 5) = InputBox("Unit Cost?")
Target.Offset(0, 6) = InputBox("Unit Weight?")
Target.Offset(0, 7) = InputBox("Weight's UoM?")
Else
Target.Offset(0, 1) = res
End If
請注意,如果您放棄,WorksheetFunction您可以在找不到匹配項的情況下跳過運行時錯誤,而是測驗回傳值以檢查發生了什么。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/519611.html
標籤:擅长vba
