6

Situation: I'm using Excel 10. I have a named variable that uses a formula to compute it's value. This is a named variable, not a named range (in the 'Name Manager' the name "MaxDate" refers to "=MAX(Sheet1!B:B)" where column B is a list of dates). The computed value does not appear in any cells by itself, but rather is used in various formulas within the spreadsheet.

My problem: How can I reference this named variable in VBA code? Using range("MaxDate").value does not work. I know I can simply put the value into a cell and reference the cell, but I'd like to find a way to reference the variable directly. Any ideas?Thanks.

2 Answers 2

15

Here's some examples of working with your named variable:

Sub TestNamedVariable()

Dim varFormula As String
Dim varValue As Variant

'This will print the formula if you need it:
varFormula = ActiveWorkbook.Names("MaxDate")

'This will evaluate the named variable/formula
varValue = Application.Evaluate("MaxDate")

End Sub
Sign up to request clarification or add additional context in comments.

Comments

2

For a given workbook with a Named variable "MyVar", valued "=MAX(Sheet1!B:B)".
Try the following code:

Evaluate(ActiveWorkbook.Names("MyVar").RefersTo)

Replace the ActiveWorkbook with the object you're referring if you need to.

2 Comments

OP stated they are not working with a named range - the name points to a formula (but not in any cell)
Agreed. Missed that it was in fact a formula. Code rewritten. @TimWilliams

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.