Perform same operation on a set of variables

neilwebber

Registered User.
Local time
Today, 23:04
Joined
Aug 19, 2002
Messages
35
Hello
After some searching here and elsewhere, I'm not sure that this is possible but.......

I have a set of string variables. I want to perform the same operation on each variable and return the result to the same variable name.

var1 = "xxxx"
var2 = "yyyy"
var3 = "zzzz"

For each variable I want the first 2 letters. I currently operate on each variable individually.
var1 = Left(var1,2)
var2 = Left(var2,2)
var3 = Left(var3,2)

How can I do this in a loop (much neater)? Perhaps via an array?

Code:
theAry = Array(var1,var2,var3)
i = 0
Do Until i > UBound(theAry)
     theAry(i) = Left(theAry(i), 2)
     'set the appropriate variable to be the new string???????
     i = i + 1
Loop
Trouble is, I can't get the new value assigned back to the variable name. My variable name is now a variable and it appears that this is not allowed.

In the real world I have a large number of string variables and the operation is more complex than a simple trim, which equates to lots of repetitive code if done on each variable individually.

grateful for any suggestions
N
 
You're right to want to use an array, but why do you need to assign things back to individually decalred variables? Why not just leave your strings in the array, you can always grow and shrink your array as needed using the redim method.
 
Yep, why not just leave my strings in the array.
Was so fixed on getting the variables back out again that I didn't think about that.
many thanks Mr DJ
 

Users who are viewing this thread

Back
Top Bottom