I found the following in Excel VBA Programming for Dummies.
Sub GetPrice()
Dim PartNum As Variant
Dim Price As Double
PartNum = InputBox("Enter the Part Number")
Sheets("Prices").Activate
Price = WorksheetFunction. _
VLookup(PartNum, Range("PriceList"), 2, False)
MsgBox PartNum & " costs " & Price
End Sub
I enter a part number and it gives me the corresponding price. If the part number I enter is a perfect match, I get the correct price. If I enter an incorrect part number, I get an error: Run-time error '1004': Unable to get the Vlookup property of the Worksheet Function Class. If I hit debug, the following is highlighted:
Price = WorksheetFunction. _
VLookup(PartNum, Range("PriceList"), 2, False)
I realize I have to use the "FALSE" command because it must find an exact part name to reference with the price. Also, if I hit cancel, it will give me the same error as if I entered an incorrect part number.
Is there any way around this? Ideally I would like no error message if the cancel button is selected, and if an incorrect part number is entered it would be great if a "wrong part number entered" message could be given instead of the Runtime Error-Help-Debug error window. Thanks in Advance. Raymond
Sub GetPrice()
Dim PartNum As Variant
Dim Price As Double
PartNum = InputBox("Enter the Part Number")
Sheets("Prices").Activate
Price = WorksheetFunction. _
VLookup(PartNum, Range("PriceList"), 2, False)
MsgBox PartNum & " costs " & Price
End Sub
I enter a part number and it gives me the corresponding price. If the part number I enter is a perfect match, I get the correct price. If I enter an incorrect part number, I get an error: Run-time error '1004': Unable to get the Vlookup property of the Worksheet Function Class. If I hit debug, the following is highlighted:
Price = WorksheetFunction. _
VLookup(PartNum, Range("PriceList"), 2, False)
I realize I have to use the "FALSE" command because it must find an exact part name to reference with the price. Also, if I hit cancel, it will give me the same error as if I entered an incorrect part number.
Is there any way around this? Ideally I would like no error message if the cancel button is selected, and if an incorrect part number is entered it would be great if a "wrong part number entered" message could be given instead of the Runtime Error-Help-Debug error window. Thanks in Advance. Raymond