VBA vlookup with Dynamic Filename. PLEASE HELP!!

NewAtVba

New Member
Joined
Jun 1, 2011
Messages
16
Hi, I'm new to VBA and I've been stuck on this problem for a few days. I've googled and checked the forums but all the solutions don't seem to work.

I'm trying to get create a macro that references an older file to update a new file that is received daily. For example, i have a master copy called master.xlsx. I receive a new file today and using vlookup i will copy over the last 4 columns to the new file by comparing the IDs(IDs are in column A). These 4 columns are comments. This new file is now the master copy. The macro will be run from the new file.

This is my current code which is very undynamic. i need to change the filename by storing it as a variable or use the indirect method.

Application.ScreenUpdating = False
Dim Bottom As Long
Bottom = Range("A65536").End(xlUp).Row

Range("r2").Select
ActiveCell.Formula = "=VLOOKUP($A2,'[Master Copy.xlsx]Sheet1'!$A$1:$t$" & Bottom & ",17,0)"
Selection.AutoFill Destination:=Range("r2:r" & Bottom)

Range("s2").Select
ActiveCell.Formula = "=VLOOKUP($A2,'[Master Copy.xlsx]Sheet1'!$A$1:$t$" & Bottom & ",18,0)"
Selection.AutoFill Destination:=Range("s2:s" & Bottom)

Range("t2").Select
ActiveCell.Formula = "=VLOOKUP($A2,'[Master Copy.xlsx]Sheet1'!$A$1:$t$" & Bottom & ",19,0)"
Selection.AutoFill Destination:=Range("t2:t" & Bottom)

Range("u2").Select
ActiveCell.Formula = "=VLOOKUP($A2,'[Master Copy.xlsx]Sheet1'!$A$1:$t$" & Bottom & ",20,0)"
Selection.AutoFill Destination:=Range("u2:u" & Bottom)



The line i tried using indirect was
ActiveCell.Formula = "=VLOOKUP($A2,INDIRECT(""x3"")]$A$1:$t$500,17,0)"
and it doesn't work

I'm open to any solutions. PLEASE help. thank you!! :)
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Welcome to MrExcel.

If X3 contains Master Copy.xlsx:

Code:
ActiveCell.Formula = "=VLOOKUP($A2,INDIRECT(""'[""&X3&""]Sheet1'!$A$1:$T$500""),17,0)"

or:

Code:
ActiveCell.Formula = "=VLOOKUP($A2,'[" & Range("X3").Value & "]Sheet1'!$A$1:$T$500,17,0)"

Note that the source workbook must be open in both cases.
 
Upvote 0
Hi! thanks for replying so quickly!

i cant get it to work if i include the Bottom variable that i am using. Is there something wrong with my syntax?

ActiveCell.Formula = "=VLOOKUP($A2,'[" & Range("X3").Value & "]Sheet1'!$A$1:$T$"&Bottom&",17,0)"
 
Upvote 0
That's:

Code:
ActiveCell.Formula = "=VLOOKUP($A2,'[" & Range("X3").Value & "]Sheet1'!$A$1:$T$" & Bottom & ",17,0)"
 
Upvote 0

Forum statistics

Threads
1,224,532
Messages
6,179,388
Members
452,908
Latest member
MTDelphis

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