Formula help needed to insert into a range

User avatar
chamdan
3StarLounger
Posts: 372
Joined: 17 Dec 2013, 00:07

Formula help needed to insert into a range

Post by chamdan »

Hi,

I am having a problem inserting this formula into the range specified here below;

Code: Select all

Range("$A$5").FormulaR1C1 = "=VLOOKUP(RngVal,Table2,2,FALSE)""
This formula works well and gives me the the result needed in the Immediate Window but when I look at the cell in Range A5 the result is #NAME? why?

You help is appreciated.

Chuck
Last edited by chamdan on 15 Jun 2016, 18:07, edited 1 time in total.

User avatar
HansV
Administrator
Posts: 78549
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Formula help needed to insert into a range

Post by HansV »

There shouldn't be an = after VLOOKUP.

Is sheetName a defined name or the name of a variable in your code?
Best wishes,
Hans

User avatar
chamdan
3StarLounger
Posts: 372
Joined: 17 Dec 2013, 00:07

Re: Formula help needed to insert into a range

Post by chamdan »

Hans,

It is a variable in my code.
Chuck

User avatar
chamdan
3StarLounger
Posts: 372
Joined: 17 Dec 2013, 00:07

Re: Formula help needed to insert into a range

Post by chamdan »

Corrected as:

Code: Select all

Range("$A$5").FormulaR1C1 = "=VLOOKUP=(RngVal,Table2,2,FALSE)"
I corrected it but it is giving me the following message;
Capture.PNG
Chuck
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78549
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Formula help needed to insert into a range

Post by HansV »

There should NOT be an = after VLOOKUP, as I mentioned in my first reply.
Best wishes,
Hans

User avatar
chamdan
3StarLounger
Posts: 372
Joined: 17 Dec 2013, 00:07

Re: Formula help needed to insert into a range

Post by chamdan »

Sorry Hans it's my mistake of pasting wrongly here below it is but still giving me the same error message????

Code: Select all

Range("$A$5").FormulaR1C1 = "=VLOOKUP(sheetName,Table2,2,FALSE)"

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Formula help needed to insert into a range

Post by Rudi »

If RngVal is a variable, you can try this:

Code: Select all

Range("$A$5").Formula = "=VLOOKUP(" & RngVal & ",Table2,2,FALSE)"
Edit:
Oh...sorry...I see you now have sheetName? (Replace RngVal above with sheetName)
Notice also its .Formula, not .FormulaR1C1
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Formula help needed to insert into a range

Post by Rudi »

Another thing.... Since your variable is now called sheetName, I assume you are passing a text string into the formula.
If this is the case, change the line to this...

Code: Select all

Range("$A$5").Formula = "=VLOOKUP(" & """" & sheetName & """" & ",Table2,2,FALSE)"
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
chamdan
3StarLounger
Posts: 372
Joined: 17 Dec 2013, 00:07

Re: Formula help needed to insert into a range

Post by chamdan »

Hans

This the result showing in the immediate window:

Code: Select all

Range("$A$5").Formula = "=VLOOKUP(" & sheetName & ",Table2,2,FALSE)"

Code: Select all

?Range("$A$5").Formula
=VLOOKUP(AAC,Table2,2,FALSE)

I want the result to be:
=VLOOKUP("AAC",Table2,2,FALSE)

I tried:

Code: Select all

Range("$A$5").Formula = "=VLOOKUP(" & """ & sheetName & """ & ",Table2,2,FALSE)"
It did not work

Chuck

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Formula help needed to insert into a range

Post by Rudi »

It's 4 double quotes on each side.
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
chamdan
3StarLounger
Posts: 372
Joined: 17 Dec 2013, 00:07

Re: Formula help needed to insert into a range

Post by chamdan »

Wow! I thought one needed 3 double quote to make a quote. Thank You both!

Resolved.

Chuck