Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default Variable Naming.....?

Hi all,

Just working on my first VBA code. A quick question please.......

When using a Counter, how can you create a Variable whose name is
based on the current value of the Counter?

For example:
________________________________________

First Iteration
***************
Counter = 1
Create a Variable whose name is 'VAR1'


Second Iternation
********************
Counter = 2
Create a Variable whose name is 'VAR2'


Third Iteration
***************
Counter = 3
Create a Variable whose name is 'VAR3'
________________________________________

I hope that makes sense.....!

Many thanks,

Jason Paris

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 133
Default Variable Naming.....?

You can't do this as written but why not use an array (look them up in VBA
help to get the full info)?

Dim myArray(20)
Counter = 1
myArray(Counter)=???
Counter=2
myArray(Counter)=???
etc

"Jason Paris" wrote:

Hi all,

Just working on my first VBA code. A quick question please.......

When using a Counter, how can you create a Variable whose name is
based on the current value of the Counter?

For example:
________________________________________

First Iteration
***************
Counter = 1
Create a Variable whose name is 'VAR1'


Second Iternation
********************
Counter = 2
Create a Variable whose name is 'VAR2'


Third Iteration
***************
Counter = 3
Create a Variable whose name is 'VAR3'
________________________________________

I hope that makes sense.....!

Many thanks,

Jason Paris


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,290
Default Variable Naming.....?

Jason,
I don't believe you can (at least within the same sub)
and it probably isn't necessary...

If Counter = 1 Then
'do something
ElseIf Counter = 2 Then
'do something else
ElseIf Counter 2 Then
'take a break
End If

Also, you can also declare a variable at the top of a standard module.
Its value would then be available in all subs/function/modules...

Public Counter as Long
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware




"Jason Paris"
wrote in message
Hi all,
Just working on my first VBA code. A quick question please.......
When using a Counter, how can you create a Variable whose name is
based on the current value of the Counter?
For example:

First Iteration
***************
Counter = 1
Create a Variable whose name is 'VAR1'
Second Iternation
********************Counter = 2
Create a Variable whose name is 'VAR2'
Third Iteration
***************
Counter = 3
Create a Variable whose name is 'VAR3'
I hope that makes sense.....!
Many thanks,
Jason Paris

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default Variable Naming.....?

You can't, but you can use an array

Dim Var(1 to 12)

Counter = 2
Msgbox Var(Counter)

--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)

"Jason Paris" wrote in message
ps.com...
Hi all,

Just working on my first VBA code. A quick question please.......

When using a Counter, how can you create a Variable whose name is
based on the current value of the Counter?

For example:
________________________________________

First Iteration
***************
Counter = 1
Create a Variable whose name is 'VAR1'


Second Iternation
********************
Counter = 2
Create a Variable whose name is 'VAR2'


Third Iteration
***************
Counter = 3
Create a Variable whose name is 'VAR3'
________________________________________

I hope that makes sense.....!

Many thanks,

Jason Paris



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 235
Default Variable Naming.....?

Possibly another option if you do intend to put "VAR" in front of the counter
number:

Sub Test()
Dim i As Variant
Cnt = 1
Do Until Cnt = 10
Cnt = Cnt + 1
Loop
i = "Var" & Cnt
msgbox i
End Sub




--
John Mansfield
http://cellmatrix.net





"Jason Paris" wrote:

Hi all,

Just working on my first VBA code. A quick question please.......

When using a Counter, how can you create a Variable whose name is
based on the current value of the Counter?

For example:
________________________________________

First Iteration
***************
Counter = 1
Create a Variable whose name is 'VAR1'


Second Iternation
********************
Counter = 2
Create a Variable whose name is 'VAR2'


Third Iteration
***************
Counter = 3
Create a Variable whose name is 'VAR3'
________________________________________

I hope that makes sense.....!

Many thanks,

Jason Paris




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Variable Naming.....?

VBA doesn't work like that.

But you can define an array and store things in the elements of that array.

Dim Counter as long
dim myArr() as long 'whatever you need

redim myarr(1 to 10)

for counter = lbound(myarr) to ubound(myarr)
myarr(counter) = 17 * counter + 32
next counter

==========
Later you can refer to any element

dim iCtr as long 'just another variable

ictr = 5 'something between 1 and 10 in my example.
msgbox myarr(ictr)



Jason Paris wrote:

Hi all,

Just working on my first VBA code. A quick question please.......

When using a Counter, how can you create a Variable whose name is
based on the current value of the Counter?

For example:
________________________________________

First Iteration
***************
Counter = 1
Create a Variable whose name is 'VAR1'

Second Iternation
********************
Counter = 2
Create a Variable whose name is 'VAR2'

Third Iteration
***************
Counter = 3
Create a Variable whose name is 'VAR3'
________________________________________

I hope that makes sense.....!

Many thanks,

Jason Paris


--

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
naming a variable range Nigel[_27_] Excel Programming 4 November 20th 06 03:23 AM
Variable Naming Convention Ben Excel Programming 0 April 8th 06 08:48 AM
Naming a worksheet as a variable in a Active Chart Series Farooq Sheri Excel Programming 5 January 26th 06 08:00 AM
Naming convention for a variable "Book1" while copying sheets JER Excel Programming 6 April 3rd 04 08:54 PM
Naming a variable in a file path Ron[_13_] Excel Programming 1 October 19th 03 02:56 AM


All times are GMT +1. The time now is 09:21 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"