VBA Code - VLOOKUP Formula with Dynamic Filename

davidh120t

New Member
Joined
Apr 17, 2015
Messages
3
I hope my favorite community can help me with a VBA problem that has been stumping me. I have a VBA macro where I want to insert a VLOOKUP formula into a cell. Sounds easy right? However, the workbook I am going to perform the lookup on is different that the one where the macro resides and I also want to use the name of that workbook (which I have captured and stored as a variable earlier) in the LOOKUP function. I have everything working until I get to the VLOOKUPand I cannot get the file name I stored to be used in the VLOOKUP function. I have placed my code below and highlighted the offending line of code. Thanks immensely in advance for any help! Also, I am trying to use a counter for the row number in that sheet which is going to be looked up.


Dim fd As FileDialog
Dim oFD As Variant
Dim fileName As String
Dim mywb As String
Dim subfile As String
Dim sheetname As String
Dim x As Integer
sheetname = "Sales"
mywb = ActiveWorkbook.Name
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.ButtonName = "Select"
.AllowMultiSelect = False
.Filters.Add "All Files", "*.*", 1
.Title = "Choose Webster Takeover File"
.InitialView = msoFileDialogViewDetails
.Show

For Each oFD In .SelectedItems
fileName = oFD
Next oFD
On Error GoTo 0
End With
Workbooks.Open (fileName)
subfile = ActiveWorkbook.Name
Workbooks(subfile).Activate
Sheets(sheetname).Select
x = 3
Cells(x, 1).Select
'Loop until a blank row is found
Do While Cells(x, 1).Value <> ""
Cells(x + 1, 1).Select
x = x + 1
Loop
Workbooks(mywb).Activate
Cells(1000, 2).Select
ActiveCell.Value = subfile
Cells(1, 3).Select
Application.CutCopyMode = False
Cells(1, 3).Select
ActiveCell.Formula = "=VLookup(A1,'(subfile)Sales'!$a$3:$b$x,2,FALSE)"
End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Try replacing the bold line in your post with this one:

ActiveCell.Formula = "=VLookup(A1," & "[" & subfile & "]Sales!$a$3:$b$x,2,FALSE)"
 
Upvote 0
Since you are entering a formula in your worksheet, the correct syntax to refer to a different workbook is

'[Book1]Sheet1 235'!$A$1

so to construct that you would need:

"'[" & varableworkbookname & "]Sheet1 235'!$A$1"
 
Upvote 0
Joe, thanks for the reply to my question but when I inserted the code as you suggested I received error message '1004' Application defined or object defined error. My code now looks like this:

Workbooks(mywb).Activate
Cells(1000, 2).Select
ActiveCell.Value = subfile
Cells(1, 3).Select
Application.CutCopyMode = False
Cells(1, 3).Select
ActiveCell.Formula = "=VLookup(A1," & " [" & subfile & "]Sales!!$a$3:$b$x,2,FALSE)"
End Sub
 
Upvote 0
Chicago, thanks for the reply to my question but when I inserted the code as you suggested I received error message '1004' Application defined or object defined error. My code now looks like this:

Workbooks(mywb).Activate
Cells(1000, 2).Select
ActiveCell.Value = subfile
Cells(1, 3).Select
Application.CutCopyMode = False
Cells(1, 3).Select
ActiveCell.Formula = "=VLookup(A1," & " [" & subfile & "]Sales'!$a$3:$b$x,2,FALSE)"
End Sub
 
Upvote 0
First of all there are 2 errors in this one line
ActiveCell.Formula = "=VLookup(A1,"'[" & subfile & "]Sales'!$a$3:$b$" & x & ",2,FALSE)"

That being said it seems like you have a lot of other errors in there. For instance what is your while loop for?

I'd suggest providing a sample of your dataset and explain what you are trying to achieve based on that.
 
Upvote 0
Joe, thanks for the reply to my question but when I inserted the code as you suggested I received error message '1004' Application defined or object defined error. My code now looks like this:

Workbooks(mywb).Activate
Cells(1000, 2).Select
ActiveCell.Value = subfile
Cells(1, 3).Select
Application.CutCopyMode = False
Cells(1, 3).Select
ActiveCell.Formula = "=VLookup(A1," & " [" & subfile & "]Sales!!$a$3:$b$x,2,FALSE)"
End Sub
Sorry, I missed a couple of apostrophes. Try this:

ActiveCell.Formula = "=VLookup(A1," & "'[" & subfile & "]Sales'!$a$3:$b$x,2,FALSE)"
 
Upvote 0

Forum statistics

Threads
1,222,827
Messages
6,168,482
Members
452,192
Latest member
FengXue

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