Using variables with a formula

gt93grad

Board Regular
Joined
Dec 7, 2011
Messages
54
Trying to use the ModelType variable to define an active range. It basically says to create "LinksFGsLookup" for a range on the "LinksFGs" worksheet. Sometimes I want to use the LinksFGs worksheet, but sometimes it's call some other form of LinksXXX. Given that ModelType is set to "FGs", why does the second line work, but the first does not? The first does not create a range name. No error, but it doesn't create a range.

ActiveWorkbook.Names.Add Name:="Links" & ModelType & "Lookup", RefersToR1C1:= _
"='Links[" & ModelType & "]'!R6C1:R" & LastRow & "C5"

ActiveWorkbook.Names.Add Name:="Links" & ModelType & "Lookup", RefersToR1C1:= _
"='LinksFGs'!R6C1:R" & LastRow & "C5"
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Try removing the brackets from:
"='Links[" & ModelType & "]'!R6C1:R" & LastRow & "C5"
 
Upvote 0
Try removing the brackets from:
"='Links[" & ModelType & "]'!R6C1:R" & LastRow & "C5"

Yep, that did it. Another example had the brackets, but maybe they were just using them to highlight the variable area. Thanks.
 
Upvote 0
Here's a similar problem, but I can't get this one to work either. "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)"
 
Upvote 0
Here's a similar problem, but I can't get this one to work either. "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)"

If StageCostRange is a range object try:
Code:
ActiveCell.FormulaR1C1 = "=IFERROR(VLOOKUP(A7&B7," & StageCostRange & ",2,FALSE),0)"
 
Upvote 0
Seems to work, but now it's treating A7&B7 as a literal, and resulting in this. How do I tell it that A7 and B7 and cells in the worksheet?

=IFERROR(VLOOKUP('A7'&'B7', StageCostFGs,2,FALSE),0)
 
Upvote 0
Seems to work, but now it's treating A7&B7 as a literal, and resulting in this. How do I tell it that A7 and B7 and cells in the worksheet?

=IFERROR(VLOOKUP('A7'&'B7', StageCostFGs,2,FALSE),0)
Copy the formula from your browser exactly as I posted it. There are no apostrophes (') in it. You said the range variable was StageCostRange, now you're showing StageCostFGs - which is it???
 
Upvote 0
Maybe I copied the wrong one just then. In any case, I remove R1C1 from the statement and it seems to work now. It didn't like "ActiveCell.FormulaR1C1" for some reason.
 
Upvote 0
Seems to work, but now it's treating A7&B7 as a literal, and resulting in this.
17.jpg
18.jpg
19.jpg
20.jpg
 
Upvote 0

Forum statistics

Threads
1,203,490
Messages
6,055,726
Members
444,814
Latest member
AutomateDifficulty

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