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!
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Re: how to write vlookup with variable in VBA?

You cant put a variable in the middle of a string and hope that Excel can see it as a variable.

Try

Code:
Range("X13").Select
    ActiveCell.FormulaR1C1 = _
        "=VLOOKUP(RC[-7],!" & dpath & "!C10:C38,29,FALSE)"
 
Upvote 0
Re: how to write vlookup with variable in VBA?

You cant put a variable in the middle of a string and hope that Excel can see it as a variable.

Try

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


I am getting "Object doesn't support this property or method" error.
How can I resolve this issue?

Thank you so much for your supprot :biggrin::biggrin:
 
Upvote 0
Re: how to write vlookup with variable in VBA?

Not a VBA expert.

Can you assign the dpath result to an integer variable and put that in?
e.g.

Code:
Dim dpath2 as integer
dpath2=dpath

Insert dpath2 into the VLOOKUP instead of dpath
 
Last edited:
Upvote 0
Re: how to write vlookup with variable in VBA?

Seems like its not allowing me to setup integer variable like that..
 
Upvote 0
Re: how to write vlookup with variable in VBA?

Sorry, just noticed dpath is a string
Dim dpath2 to a string
 
Upvote 0
Re: how to write vlookup with variable in VBA?

Hi,

I was able to setup the variable, but I am getting the different error. "Application-defined or object-defined error"
Do see any flaws from my formula?

Thank you!:biggrin:
 
Upvote 0
Re: how to write vlookup with variable in VBA?

What is your value of "dpath"?
What does a good VLOOKUP formula on your sheet looking up that range look like?
That is what you need to build, exactly.

Also note:
Your current structure is inefficient and will actually slow your code down. You do not need to select a range before applying a formula or value to it.
Just structure it like this one line:
Code:
   Range("X13").FormulaR1C1 = ...
 
Last edited:
Upvote 0
Re: how to write vlookup with variable in VBA?

the value of dpath is
C:\Users\Projects\2018\REPORTS\2018_04Report.xlsx
and good vlookup looks like

<tbody>
</tbody>
The correct vlookup is this:
Code:
=VLOOKUP(Q13,'[2018_04Report.xlsx]main_report'!$J:$AL,29,FALSE)
However workbook name changes depends on what user puts value into C6(dpath).

And thanks for the tip on optimizing the code!
 
Last edited:
Upvote 0
Re: how to write vlookup with variable in VBA?

However workbook names changes depends what value user puts into C6.
So, for this particular example, exactly what would be in cell C6?
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,717
Members
448,985
Latest member
chocbudda

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