ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Sequential variables (no array) (https://www.excelbanter.com/excel-programming/453705-sequential-variables-no-array.html)

[email protected]

Sequential variables (no array)
 
Hi,
is possible in the code using a for next for assign a value to a sequential variables (no array)?

I mean, instead of this...

aa1 = 9
aa2 = 9
aa3 = 9
.......
aa100 = 9

use:

for x = 1 to 100
aa&"x" = 9 -- I do not know the correct syntax !!!
next x

No array aa(x) please.

Thanks to all
R.

Claus Busch

Sequential variables (no array)
 
Hi,

Am Mon, 31 Jul 2017 01:35:55 -0700 (PDT) schrieb :

aa1 = 9
aa2 = 9
aa3 = 9
......
aa100 = 9


you don't need a loop.
Try:
Range("AA1:AA100")=9


Regards
Claus B.
--
Windows10
Office 2016

[email protected]

Sequential variables (no array)
 
Il giorno lunedì 31 luglio 2017 10:41:29 UTC+2, Claus Busch ha scritto:
Hi,

Am Mon, 31 Jul 2017 01:35:55 -0700 (PDT) schrieb:

aa1 = 9
aa2 = 9
aa3 = 9
......
aa100 = 9


you don't need a loop.
Try:
Range("AA1:AA100")=9


Regards
Claus B.
--
Windows10
Office 2016


Thanks Claus,
but they are vba variables not cells.

Bye

Auric__

Sequential variables (no array)
 
reinquisito2 wrote:

is possible in the code using a for next for assign a value to a sequential
variables (no array)?

I mean, instead of this...

aa1 = 9
aa2 = 9
aa3 = 9
......
aa100 = 9

use:

for x = 1 to 100
aa&"x" = 9 -- I do not know the correct syntax !!!
next x

No array aa(x) please.


Why not? This is one of the reasons arrays exist.

There is nothing built into VBA that will let you do as you ask. You could
probably do this using, say, self-modifying code, but that opens a whole new
can of worms, and is definitely not for beginners.

What *exactly* are you trying to accomplish? *Why* don't you want to use an
array?

--
- I'm going to free them.
- Why?
- Because it's the right thing to do, you narrow-minded jackass.

[email protected]

Sequential variables (no array)
 
Il giorno lunedì 31 luglio 2017 12:55:06 UTC+2, Auric__ ha scritto:
reinquisito2 wrote:

is possible in the code using a for next for assign a value to a sequential
variables (no array)?

I mean, instead of this...

aa1 = 9
aa2 = 9
aa3 = 9
......
aa100 = 9

use:

for x = 1 to 100
aa&"x" = 9 -- I do not know the correct syntax !!!
next x

No array aa(x) please.


Why not? This is one of the reasons arrays exist.

There is nothing built into VBA that will let you do as you ask. You could
probably do this using, say, self-modifying code, but that opens a whole new
can of worms, and is definitely not for beginners.

What *exactly* are you trying to accomplish? *Why* don't you want to use an
array?

--
- I'm going to free them.
- Why?
- Because it's the right thing to do, you narrow-minded jackass.


Ok Auric,
I rewrite code using array.
Thanks
BR

[email protected]

Sequential variables (no array)
 

There is nothing built into VBA that will let you do as you ask.


One way, if variables names must beginning with underscore char (to avoid problems with cell names) is to use "named ranges":

Sub createNames()
'run only one time

Dim j As Long

For j = 1 To 100
ThisWorkbook.Names.Add Name:="_aa" & j, RefersTo:=0, Visible:=False
Next

End Sub

Sub writeNames()
Dim j As Long

'run only one time
For j = 1 To 100
ThisWorkbook.Names("_aa" & j).RefersTo = 9
Next


End Sub

Sub readNames()
Dim j As Long

'run only one time
For j = 1 To 10
Debug.Print Application.Evaluate(ThisWorkbook.Names("_aa" & j).RefersTo)
Next


End Sub


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

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com