VBA Vlookup

superfb

Active Member
Joined
Oct 5, 2011
Messages
251
Office Version
  1. 2007
Platform
  1. Windows
Hi

i am currently learning VBA, and something that i thought should be easy to execute is proving to be a pain.....



Sub TestVlookup()


Dim Product As Variant
Dim Price As Double


Product = InputBox("What code are you looking for?")


Worksheets("Database").Activate


Price = WorksheetFunction.VLookup(Product, Range("Price"), 2, False)


MsgBox ("The cost of the product" & Product & " is" & Price)


Database sheet:

ProductPrice
231.99
2425.64
2513.98
2611
2714.79

<colgroup><col width="64" span="2" style="width:48pt"> </colgroup><tbody>
</tbody>



I keep getting a 1004 error......Which is being caused by the vlookup- i have defined the variables and labeles the data sources (name range) as accordingly, but unable to figure out what is going on?
 
These
Code:
Dim Products As Long
Dim Prices As Double
Should both be Variant
Code:
Dim Products As Variant
Dim Prices As Variant
 
Upvote 0

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
These
Code:
Dim Products As Long
Dim Prices As Double
Should both be Variant
Code:
Dim Products As Variant
Dim Prices As Variant


Hi, i did change it to variant, now the error message says "Type Mismatch"...i believe this does that because if i type in a number that is not in the database then it comes with this error message....
 
Last edited:
Upvote 0
If the serach value is not found, you will not get an error.
It sounds more like it has found the value, but the returned value from column 2 is a formulaic error such as #N/A, #NAME, #VALUE etc
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,006
Messages
6,122,666
Members
449,091
Latest member
peppernaut

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