Run -time error '1004' Method 'Range' of object'_Global' failed

GJennin7

New Member
Joined
Oct 7, 2020
Messages
11
Office Version
  1. 365
Platform
  1. Windows
Can anyone shed some light on the below run time error for the line highlighted. I am a newby guy's so don't get too technical on me. Thanks for any advice.
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
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
That error indicates that Price is returned as an error, likely because PartNum can't be found. To avoid the run time error, try it like this;
VBA Code:
Sub GetPrice()
Dim PartNum As Variant
Dim Price As Variant
PartNum = InputBox("Enter the Part Number")
Sheets("Prices").Activate
Price = Application.VLookup(PartNum, Range("PriceList"), 2, False)
If IsError(Price) Then
    MsgBox "Can't find Part Num " & PartNum
Else
    MsgBox PartNum & " costs " & CDbl(Price)
End If
End Sub
NOTE that Price has been dimensioned as a Variant not as Double. That allows Price to be an error if PartNum not found or a double if PartNum is found.
 
Upvote 0
I'm going to Guess there is no range named "PriceList"
Thanks Dave, You pushed me in the right direction. I did not define my range name and when I did initially incorrectly but persistence paid off.
 
Upvote 0

Forum statistics

Threads
1,214,644
Messages
6,120,709
Members
448,983
Latest member
Joaquim_Baptista

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top