2

I have a load of variable names in a spreadsheet column. These variables are defined and have values in the modules I have in my project.

I want to have a code that references the variable name in a spreadsheet, and returns the value that it have in the module and pastes the value in another spreadsheet i.e

Sub code()

    dim variable1 as integer
    variable1 = 2 

End sub

sheet 1: cell A1: variable1

Sub code2()
    sheet(2).range("a1").value = sheet(1).range("a1").value
end sub

sheet 2: cell A1: 2
3
  • your question is unclear when you describe 'variable names in a spreadsheet column'. What does it mean? How does it look like? Commented Aug 20, 2013 at 13:53
  • column a: variable1, variable2, variable3, in a1,a2,a3 respectively these are the 'variable names', and these variables have values. i.e 1,2,3 taken from the module, in a sub procedure. I would like a code that looks up these variable names in the spreadsheet and returns the values... Commented Aug 20, 2013 at 14:06
  • 2
    it's still a bit unclear. However, additionally you could try to name your cells in this way that Sheet1, Cell A1 will have variable1 name. Next you will need to check/set its value using the following reference: Sheets("Sheet1").Range("Variable1").Value. But you CAN NOT do it in this way: Sheets("Sheet1").Range(Variable1).Value. Alternatively, check if VlookUp function will not be the one you look for. Commented Aug 20, 2013 at 14:29

2 Answers 2

4

There is no way to ask for a variable by name in VBA during runtime. During compilation all variable names are stripped away, and at runtime the variables are referenced just with memory locations. Also, if the variable is declared within a sub, it only exists while that sub is being executed. If you try to access it later, something else will be using its memory location.

The only way to do this is to declare all the variables at module level, and then have a function which explicitly maps variable names to these variables:

Private variable1 As Integer

Sub code()
    variable1 = 2
End Sub

Sub code2()
    Sheets(2).Range("a1").Value = VariableByName(Sheets(1).Range("a1").Value)
End Sub

Function VariableByName(VarName As String) As Variant
    Select Case VarName
        Case "variable1": VariableByName = variable1
    End Select
End Function

Actually, your best option is to forget about using variables and use names instead:

Sub code()
    Names.Add "variable1", 2, Visible:=False
End Sub

Sub code2()
    Sheets(2).Range("a1").Value = Evaluate(Sheets(1).Range("a1").Value)
End Sub

But when you go that route, if you need to access the variable in VBA you can't just say variable1, you need to use code like this:

Sub code3()
    Dim variable1 As Integer

    variable1 = Evaluate("variable1") 'bring it into a normal variable
    variable1 = variable1 * 2 'now you can use it like a normal variable
    Names("variable1").RefersTo = variable1 'need to save it when you're done
End Sub
Sign up to request clarification or add additional context in comments.

Comments

0

This worked in Excel 2010

variable1 = [variable1].Value

VBA treats [variable1] (with brackets) as a variant that references the named cell.

-mmh

Comments

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.