Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.worksheet.functions
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.

  #2   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 35,218
Default Use a string as a Variable Name

VBA doesn't work like that.

Maybe you could use a real array:

Dim BMLeft(1 to 3) as double 'I wouldn't use Single
dim myIndex as long

BMLeft(1) = 10.2
BMLeft(2) = 18.7
BMLeft(3) = 26.4

myIndex = 2
msgbox mbleft(myindex)



Brandt wrote:

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.


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 33
Default Use a string as a Variable Name

Thanks Dave

That's what I was afraid of. The array was my backup plan. And sorry for
posting this in the wrong group - not sure what I was thinking.

"Dave Peterson" wrote:

VBA doesn't work like that.

Maybe you could use a real array:

Dim BMLeft(1 to 3) as double 'I wouldn't use Single
dim myIndex as long

BMLeft(1) = 10.2
BMLeft(2) = 18.7
BMLeft(3) = 26.4

myIndex = 2
msgbox mbleft(myindex)



Brandt wrote:

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.


--

Dave Peterson

  #4   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 947
Default Use a string as a Variable Name

myString = "BM" & index & "Left"
Left = Evaluate(myString)


The Array approach is the way to go.
Just throwing out an inefficient idea here...

Sub Demo()
Dim indx As Double

With ActiveWorkbook.Names
.Add "BM1Left", 10.2
.Add "BM2Left", 18.7
.Add "BM3Left", 26.4
End With

indx = 2

MsgBox Evaluate("BM" & indx & "Left")
End Sub

--
Dana DeLouis


"Brandt" wrote in message
...
Thanks Dave

That's what I was afraid of. The array was my backup plan. And sorry for
posting this in the wrong group - not sure what I was thinking.

"Dave Peterson" wrote:

VBA doesn't work like that.

Maybe you could use a real array:

Dim BMLeft(1 to 3) as double 'I wouldn't use Single
dim myIndex as long

BMLeft(1) = 10.2
BMLeft(2) = 18.7
BMLeft(3) = 26.4

myIndex = 2
msgbox mbleft(myindex)



Brandt wrote:

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.


--

Dave Peterson



  #5   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 33
Default Use a string as a Variable Name

Thanks Dana

That's an interesting idea. I appreciate the response.



"Dana DeLouis" wrote:

myString = "BM" & index & "Left"
Left = Evaluate(myString)


The Array approach is the way to go.
Just throwing out an inefficient idea here...

Sub Demo()
Dim indx As Double

With ActiveWorkbook.Names
.Add "BM1Left", 10.2
.Add "BM2Left", 18.7
.Add "BM3Left", 26.4
End With

indx = 2

MsgBox Evaluate("BM" & indx & "Left")
End Sub

--
Dana DeLouis


"Brandt" wrote in message
...
Thanks Dave

That's what I was afraid of. The array was my backup plan. And sorry for
posting this in the wrong group - not sure what I was thinking.

"Dave Peterson" wrote:

VBA doesn't work like that.

Maybe you could use a real array:

Dim BMLeft(1 to 3) as double 'I wouldn't use Single
dim myIndex as long

BMLeft(1) = 10.2
BMLeft(2) = 18.7
BMLeft(3) = 26.4

myIndex = 2
msgbox mbleft(myindex)



Brandt wrote:

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.

--

Dave Peterson






Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Setting a string variable as the file name [email protected] Excel Discussion (Misc queries) 1 March 28th 07 11:10 PM
Nested String Filename Variable ed9213 Excel Worksheet Functions 9 February 12th 07 02:27 AM
find a variable text string [email protected] Excel Discussion (Misc queries) 2 January 30th 07 07:20 PM
calling variable within string [email protected] Excel Worksheet Functions 8 May 4th 06 03:07 PM
Variable in string DevinC Excel Discussion (Misc queries) 5 January 26th 06 08:59 PM


All times are GMT +1. The time now is 08:09 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"