How to use variable on VLOOKUP-table_array

ExcelPupper

Board Regular
Joined
Mar 2, 2020
Messages
112
Office Version
  1. 2019
Platform
  1. Windows
Is it possible to use a variable on table_array for a Vlookup formula on vba?
Im trying to run my code below but it gives error.

VBA Code:
Sub LookUpData()
'
'
'
' Keyboard Shortcut: Ctrl+Shift+Q
'
   
    Application.ScreenUpdating = False
    Dim MyFile As String, MyWB As String, ws As Worksheet

    MyLoc = "C:\Users\ADMIN\Desktop\Test\vba\"
    MyWB = MyFile & "SourceData.xlsm"
   
   
  
    Set ws = ThisWorkbook.Sheets("Data")
   
    ws.Range("C1").Formula = "=VLOOKUP($A1,'MyWB'!$B:$D,3,FALSE)"
   
   
End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Yes it is, with the name as a string you need to use

"=VLOOKUP($A1,'" & MyWB & "'!$B:$D,3,FALSE)"
 
Upvote 0
Yes it is, with the name as a string you need to use

"=VLOOKUP($A1,'" & MyWB & "'!$B:$D,3,FALSE)"

It works on my trial file but when I tried it to the actual data I use, it takes time to execute. Maybe because the source data for my table_array is big? (it's around 60MB and currently it has 640K + rows of data)
Do you have other suggestion that would speed up this macro?
 
Upvote 0
With that volume of data anything is going to be slow. How many rows is the formula being added to?
 
Upvote 0
With that volume of data anything is going to be slow. How many rows is the formula being added to?
It varies. But maximum number of rows I've encountered before was around 400rows and 3 columns.
 
Upvote 0
If you're not already doing so I would suggest setting calculation to manual and screen updating to false while the code runs, that should speed it up a bit but with ~1200 formulas looking at 640k rows of data I would expect a significant wait for it to complete.

There may be other ways that will work faster, but not ones that I am familiar with.
 
Upvote 0
Also Im having problem on what sheet the table_array should look up to because I only declared the FileName on this line:
VBA Code:
ws.Range("C1").Formula = "= VLOOKUP($A1,'" & MyWB & "'!$B:$D,3,FALSE) "
 
Upvote 0
Just append it to the file name, e.g. with the sheet name declared as a text string and assigned to MyWS
VBA Code:
ws.Range("C1").Formula = "= VLOOKUP($A1,'[" & MyWB &"]" & MyWS & "'!$B:$D,3,FALSE)"
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,683
Members
449,116
Latest member
HypnoFant

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