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

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
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,214,653
Messages
6,120,749
Members
448,989
Latest member
mariah3

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