PC Review


Reply
Thread Tools Rate Thread

Concatenate a Const Name in Sub Routine

 
 
Brandt
Guest
Posts: n/a
 
      26th Nov 2007
I have declared several constants at the top of Module1 such as:

Option Explicit
Public Const BM1Left As Single = 10.1
Public Const BM2Left As Single = 20.1
Public Const BM3Left As Single = 30.1
Etc..

In the subroutine I would like to access them with a counter via
concatenating the name such as:

Sub Test1()
Dim Counter As Integer
Dim Left As Single
Counter = 1
Left = "BM" & Counter & "Left"
End Sub

Where counter could be set by a For loop or otherwise. (So that in the
above case the variable Left = BM1Left = 10.1) The above code does not work
because the expression is evaluated as a string rather than the name of the
Constant I want. Does anyone know how I could access the Const names similar
to the "indirect" worksheet function?

Thanks in advance
 
Reply With Quote
 
 
 
 
Mike H.
Guest
Posts: n/a
 
      26th Nov 2007
Could you not just say:



If counter=1 then
left=10.1
elseif counter=2 then
left=20.1
....etc

or just
left=(counter*10)+.1



 
Reply With Quote
 
Brandt
Guest
Posts: n/a
 
      26th Nov 2007
Thanks for the reply Mike. I guess I had over simplified my post. The
numbers are random (I am sorry I implied that (counter*10)+.1 would work) and
there are enough of them that I'd like to avoid using an "IF" or "Select
Case" statement (hence the constant declaration).

Any other ideas?

"Mike H." wrote:

> Could you not just say:
>
>
>
> If counter=1 then
> left=10.1
> elseif counter=2 then
> left=20.1
> ...etc
>
> or just
> left=(counter*10)+.1
>
>
>

 
Reply With Quote
 
Mike H.
Guest
Posts: n/a
 
      26th Nov 2007
I tried using the evaulate function in conjunction with indirect but could
not get it to work. But if you would explain what you're trying to do,
perhaps I might have a different approach. I have not used constants at all
and have never felt like I needed to.

 
Reply With Quote
 
Brandt
Guest
Posts: n/a
 
      26th Nov 2007
I am creating drawing objects on the active sheet such as:
with Activesheet.Shapes
.AddShape(msoShapeRectangle, Left, Top, , 100).Name = "BM"
The drawings are created when the user clicks a paticular button on the
sheet. The click event of the button initializes the counter to the
appropriate value (1 thru 20 lets say). The drawing object would then be
placed at the appropriate "Left" position by setting
Left = "BM" &Counter &"Left"
such that if button 18 was clicked "Left" should be set equal to the value
of BM18Left. (There will also be such variables for "Top", "Width", etc) I
would like the BMxxLeft variables as Const's because that makes it much
easier to adjust them as the code progresses and changes. But either way I
do it, it would be much easier to concatenate the name as mentioned above
then to use a "IF" statement or something.

Thanks again for your help and any ideas.

"Mike H." wrote:

> I tried using the evaulate function in conjunction with indirect but could
> not get it to work. But if you would explain what you're trying to do,
> perhaps I might have a different approach. I have not used constants at all
> and have never felt like I needed to.
>

 
Reply With Quote
 
Bernd P
Guest
Posts: n/a
 
      30th Nov 2007
On 26 Nov., 15:29, Brandt <Bra...@discussions.microsoft.com> wrote:
> I have declared several constants at the top of Module1 such as:
>
> Option Explicit
> Public Const BM1Left As Single = 10.1
> Public Const BM2Left As Single = 20.1
> Public Const BM3Left As Single = 30.1
> Etc..
>

Hello,

I suggest to declare
Const BMLeft(1 to 3) as single

Then initialize in a dedicated sub:
BMLeft(1) = 10.1
....

Later on you can assign:
Left = BMLeft(Counter)


Regards,
Bernd
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Casting from non-const to const (C2440) RalphTheExpert Microsoft VC .NET 1 14th Nov 2005 07:48 PM
VBA routine in detail on print routine causing errors in report Jeff Wimer Microsoft Access Reports 0 21st Oct 2005 01:54 AM
friend CStringT operator+(const CStringT& str1, const CStringT& st =?Utf-8?B?VG9ueU0=?= Microsoft VC .NET 2 3rd Nov 2004 01:09 PM
My audit routine routine fails. Run-time error '3622' "You must use the dbSeeChanges..." Adam Microsoft Access Form Coding 1 23rd Oct 2003 12:11 AM
Re: crash changing const to public const BrianB Microsoft Excel Programming 0 4th Aug 2003 10:13 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:23 AM.