View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.worksheet.functions
Brandt Brandt is offline
external usenet poster
 
Posts: 33
Default Use a string as a Variable Name

First let me apologize for reposting this question. I didn't get any help
yesterday and am hoping a fresh set of eyes will do the trick.

I have a large number of Constants defined sequentially as such :
Public Const BM1Left As Single = 10.2
Public Const BM2Left As Single = 18.7
Public Const BM3Left As Single = 26.4
and so on...

There are enough of these that I would prefer not to use an IF statement or
a Select Case construction to access the desired const. I would be great to
be able to access the particular constant by concatenating or otherwise
"constructing" the name such as in the code below:

Sub Test(index as integer)
Dim Left As Single
Dim myString As String
myString = "BM" & index & "Left"
Left = Evaluate(myString)
End Sub

I get a "type mismatch" with the above code because myString is a string and
Left is a Single. I have searched the discussion groups for 2 days now and
tried every variation I can think of unsuccessfully. Is there a way to get
the variable Left to recognize the name of the Const represented by the
string rather than the just the string itself? I know that named ranges on a
worksheet and buttons on a user form can be evaluated in a similar manner,
but I can not figure out how to do it in this case.

Thanks for any help you can give me.