Vlookup in VBA

bobkap

Board Regular
Joined
Nov 22, 2009
Messages
241
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

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.

Scott T

Well-known Member
Joined
Dec 14, 2016
Messages
2,616
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
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]
 

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
14,050
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
Try...


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

bobkap

Board Regular
Joined
Nov 22, 2009
Messages
241
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
  3. Web
THANKS!!! That works beautifully!!
 

bobkap

Board Regular
Joined
Nov 22, 2009
Messages
241
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
  3. Web
Got it! Makes sense. I should have thought of that already. Thanks very much!
 

Watch MrExcel Video

Forum statistics

Threads
1,129,751
Messages
5,638,151
Members
417,010
Latest member
jnuss03

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
Top