Vlookup in VBA

bobkap

Active Member
Joined
Nov 22, 2009
Messages
313
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
  3. Web
I need to use a vlookup in my macro. I do NOT want to activate the worksheet as shown here in my code below. This line works, but darned if I can figure out the way to write the code that will work without first activating that worksheet.

Sheets("Promo").Activate
Custname = Application.WorksheetFunction.VLookup(procode, Range(Cells(1, 1), Cells(62, 2)), 2, False)

I've tried this line below but I got an error:


Custname = Application.WorksheetFunction.VLookup(procode, Sheets("Promo").Range(Cells(1, 1), Cells(62, 2)), 2, False)


Any help would be greatly appreciated.
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
If the file is open then you need to specifiy the workbook and sheet for each range object (Range and Cell). If you want to can declare variables so you do not have to type out everything
Code:
[COLOR=#333333]Custname = Application.WorksheetFunction.VLookup(procode, workbooks("namehere.xlsx").Sheets("sheetnamehere").Range([/COLOR][COLOR=#333333]workbooks("namehere.xlsx").Sheets("sheetnamehere").[/COLOR][COLOR=#333333]Cells(1, 1),[/COLOR][COLOR=#333333]workbooks("namehere.xlsx").Sheets("sheetnamehere").[/COLOR][COLOR=#333333] Cells(62, 2)), 2, False)[/COLOR]
 
Upvote 0
Try...


Code:
 Custname = Application.WorksheetFunction.VLookup(procode, Sheets("Promo").Range(Sheets("Promo").Cells(1, 1), Sheets("Promo").Cells(62, 2)), 2, False)
 
Upvote 0
THANKS!!! That works beautifully!!
 
Upvote 0
Got it! Makes sense. I should have thought of that already. Thanks very much!
 
Upvote 0

Forum statistics

Threads
1,215,002
Messages
6,122,652
Members
449,092
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