ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Create dynamic variable names? (https://www.excelbanter.com/excel-programming/283655-create-dynamic-variable-names.html)

J Kruger

Create dynamic variable names?
 
Hi,

I have been looking everywhere on how to create dynamic variables in excel.
I want the same result as what the eval() function in JavaScript gives. ie.

a1 = new Array(10);
a1[0] = "hello";
alert(eval("a" + 1)[0]);

or

for(i = 0; i <=10; i++){
eval("a"+i) = i;}

Any help would be appreciated.

Rob Bovey

Create dynamic variable names?
 

There's no way to do this is VB/VBA. It's just not a supported feature.

--
Rob Bovey, MCSE, MCSD, Excel MVP
Application Professionals
http://www.appspro.com/

* Please post all replies to this newsgroup *
* I delete all unsolicited e-mail responses *


"J Kruger" wrote in message
m...
Hi,

I have been looking everywhere on how to create dynamic variables in

excel.
I want the same result as what the eval() function in JavaScript gives.

ie.

a1 = new Array(10);
a1[0] = "hello";
alert(eval("a" + 1)[0]);

or

for(i = 0; i <=10; i++){
eval("a"+i) = i;}

Any help would be appreciated.




Tim Williams[_3_]

Create dynamic variable names?
 
If you want to reference items/values via "names" created at runtime then
try using a Dictionary object.
Creating variable names at run-time (even when it's possible) is usually not
the best approach to a problem.

Tim.

"J Kruger" wrote in message
m...
Hi,

I have been looking everywhere on how to create dynamic variables in

excel.
I want the same result as what the eval() function in JavaScript gives.

ie.

a1 = new Array(10);
a1[0] = "hello";
alert(eval("a" + 1)[0]);

or

for(i = 0; i <=10; i++){
eval("a"+i) = i;}

Any help would be appreciated.




J Kruger

Create dynamic variable names?
 
Thanks.

The dictionary option unfortunately can only one value/reference per
key.
So you are stuck again with creating an array or variable for each key
to contain more than one value.
Then you must in any case again make dynamic arrays/variables.

I will keep suffering with VB until it becomes a REAL language.

Tim Williams

Create dynamic variable names?
 
If you were to describe what you're trying to do I'm sure you will get at
least one viable suggestion which does not involve creating dynamic
variables. Even in languages where this is possible (eg.
javascript/vbscript) there are few occasions where it's really warranted.

Tim.


"J Kruger" wrote in message
om...
Thanks.

The dictionary option unfortunately can only one value/reference per
key.
So you are stuck again with creating an array or variable for each key
to contain more than one value.
Then you must in any case again make dynamic arrays/variables.

I will keep suffering with VB until it becomes a REAL language.





All times are GMT +1. The time now is 01:40 PM.

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