Write vlookup with variable in VBA?

jaik22

Board Regular
Joined
Sep 23, 2016
Messages
102
Hi,

I have this variable to open up the file path in C6 in MAIN tab.
Code:
Dim TESTWB As Workbook: Dim dpath As String: dpath = Template.Worksheets("MAIN").Range("C6").Value

but I am trying to do lookup with variables, but it not working.


Code:
           Range("X13").Select
    ActiveCell.FormulaR1C1 = _
        "=VLOOKUP(RC[-7],&dpath&!C10:C38,29,FALSE)"

Here is what I wrote to make vlookup. Does any sees why this code is not working?
Any advises are welcome!
 
Re: how to write vlookup with variable in VBA?

is this path below:
C:\Users\Projects\2018\REPORTS\2018_04Report.xlsx

<tbody>
</tbody>

Thank you!
 
Upvote 0

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Re: how to write vlookup with variable in VBA?

Note how your good VLOOKUP function does NOT have the whole file path. It only has the file name, and square brackets. So you need to pull that file name out of there and build the structure.
THAT is what you need to build, something like this:
Code:
    Dim dfile As String
    dfile = Mid(dpath, InStrRev(dpath, "\") + 1)
    Range("X13").FormulaR1C1 = "=VLOOKUP(Q13,'[" & dfile & "]main_report'!$J:$AL,29,FALSE)"
 
Upvote 0

Forum statistics

Threads
1,215,022
Messages
6,122,716
Members
449,093
Latest member
Mnur

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