Using a variable in a formula

gt93grad

Board Regular
Joined
Dec 7, 2011
Messages
54
I can't get this statement to work with a varaible. "StageCostRange" is a variable. I can't figure out where the quotations and apostrophes should go.

ActiveCell.FormulaR1C1 = "=IFERROR('VLOOKUP(A7&B7, " & StageCostRange & " ,2,FALSE)',0)"
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
I think you just need to remove the single quotes (apostrophes) before VLOOKUP and after FALSE).
 
Last edited:
Upvote 0
Closer. This is what I have now:

ActiveCell.FormulaR1C1 = "=IFERROR('VLOOKUP(A7&B7, " & StageCostRange & " ,2,FALSE'),0)"

However, it's treating the A7&B7 as a literal, by inserting apostrophes: 'A7'&'B7'
That's preventing the lookup from working. How can I tell it that A7 and B7 and cells in the spreadsheet, and not literals?
 
Upvote 0
Yeah, this is what I'm using, but it's still treating A7 and B7 as literals. I'm using no apostrophes now.

ActiveCell.FormulaR1C1 = "=IFERROR(VLOOKUP(A7&B7, " & StageCostRange & " ,2,FALSE),0)"
 
Upvote 0
What is the value of StageCostRange ?
Can you post the code that gives that variable it's value?

Drop the R1C1 and just use .Formula
 
Upvote 0
Yeah, this is what I'm using, but it's still treating A7 and B7 as literals. I'm using no apostrophes now.

ActiveCell.FormulaR1C1 = "=IFERROR(VLOOKUP(A7&B7, " & StageCostRange & " ,2,FALSE),0)"

After executing this line of code, switch back to Excel, click in the formula bar (the whole formula should be selected), hit Ctrl-C to copy it, hit Esc to get out of edit mode, and paste the result here. That is, what is the formula that the cell ends up having?
 
Upvote 0
Glad to help, thanks for the feedback.

The reason it faild is because when you use FormulaR1C1, then it expects a formula written in R1C1 style references.
But you were giving it a formula written in A1 style.
 
Upvote 0

Forum statistics

Threads
1,203,581
Messages
6,056,180
Members
444,850
Latest member
dancasta7

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