我正在研究一个子,它需要我制作它,以便用户可以使用输入框搜索他们的 ProductID,然后用 msgbox 让他们知道它是否被找到,但我无法正确获取代码。我在这里做错了什么?完全丢失(下面的代码):
Sub test()
Dim Worksheet As Range
Dim ProductID As Variant
ProductID = InputBox("Please enter the Product ID")
With Worksheets(1).Range("A1:Z1000")
Set ProductID = .Find("ProductID", LookIn:=xlWhole)
If found Then
MsgBox ProductID("was Found")
Else
MsgBox ProductID & (" was NOT Found")
End If
End With
End Sub
几个问题,包括以下内容:
- 将
Find
的结果分配给Range
变量(与ProductId
分开),例如变量found
。 - 使用
If Not
所述变量Is Nothing
来测试查找是否成功。 - 删除
Dim Worksheet As Range
。 - 变量不属于引号内。
ProductID
(不带引号)是变量。"ProductID"
是文本"产品 ID"。使用变量而不是文本调用Find
。 - 应该是
LookIn:=xlValues
或LookIn:=xlFormulas
,然后LookAt:=xlWhole
。
Sub test()
Dim ProductID As Variant
ProductID = InputBox("Please enter the Product ID")
Dim found As Range
Set found = Worksheets(1).Cells.Find(What:=ProductID, LookIn:=xlValues, LookAt:=xlWhole)
If Not found Is Nothing Then
MsgBox ProductID & " was Found"
Else
MsgBox ProductID & " was NOT Found"
End If
End Sub