469,602 Members | 1,984 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,602 developers. It's quick & easy.

Incrementing a variable name

I have a number of variables, e.g. X1, X2, X3, X4, X5.

I want to select only the ones that are non zero(there will only be 2 or 3 at any time), and record them as Y1, Y2 and Y3

I want to know the best way of incrementing the name of the variable, e.g. the variable in a loop is X + (num)

I was trying

Num1=1
Num2=1

Do until num1 =5
If X(num1)<>0 then Y(num2) = X(num1) else goto J1
num2 = num2 +1
J1:
num1 = num1 +1
loop

Is this the best way? and if so how do I define X(num1), I am currently getting the error "sub of function not defined"
Jul 6 '07 #1
3 3089
kadghar
1,295 Expert 1GB
I have a number of variables, e.g. X1, X2, X3, X4, X5.

I want to select only the ones that are non zero(there will only be 2 or 3 at any time), and record them as Y1, Y2 and Y3

I want to know the best way of incrementing the name of the variable, e.g. the variable in a loop is X + (num)

I was trying

Num1=1
Num2=1

Do until num1 =5
If X(num1)<>0 then Y(num2) = X(num1) else goto J1
num2 = num2 +1
J1:
num1 = num1 +1
loop

Is this the best way? and if so how do I define X(num1), I am currently getting the error "sub of function not defined"
yeap, try with an array, you can dim it of any size and redim its size.

for example you can do:

Expand|Select|Wrap|Line Numbers
  1. Dim myArray(1 to 5) as integer
  2. Sub Numbers()
  3. Dim NoZero()
  4. dim i as integer
  5. dim j as integer
  6.  
  7. myArray(1)=0
  8. myArray(2)=4
  9. myArray(3)=5
  10. myArray(4)=8
  11. myArray(5)=0
  12. j=0
  13. for i = 1 to 5
  14.     if myArray(i) <> 0 then
  15.        j= j+1
  16.     end if
  17. next
  18. ' j will be the number of no zeros in you array
  19.  
  20. if j <> 0 then 
  21.     redim NoZero(1 to j )  'here you redim the second array
  22. else
  23.     msgbox("all numbers are zero")  ' or send an error message
  24. end if
  25.  
  26. j=1
  27.  
  28. for i = 1 to 5
  29.     if myArray(i) <> 0 then
  30.        NoZero(j) = myArray(i)
  31.        j=j+1
  32.     end if
  33. next
  34. 'here you put the nozero values to the second array
  35. End Sub
It's not a great code, but it'll give you an idea of how arrays, dim and redim works out.

Hope that helps
Jul 6 '07 #2
Thanks will give it a go.
yeap, try with an array, you can dim it of any size and redim its size.

for example you can do:

Expand|Select|Wrap|Line Numbers
  1. Dim myArray(1 to 5) as integer
  2. Sub Numbers()
  3. Dim NoZero()
  4. dim i as integer
  5. dim j as integer
  6.  
  7. myArray(1)=0
  8. myArray(2)=4
  9. myArray(3)=5
  10. myArray(4)=8
  11. myArray(5)=0
  12. j=0
  13. for i = 1 to 5
  14.     if myArray(i) <> 0 then
  15.        j= j+1
  16.     end if
  17. next
  18. ' j will be the number of no zeros in you array
  19.  
  20. if j <> 0 then 
  21.     redim NoZero(1 to j )  'here you redim the second array
  22. else
  23.     msgbox("all numbers are zero")  ' or send an error message
  24. end if
  25.  
  26. j=1
  27.  
  28. for i = 1 to 5
  29.     if myArray(i) <> 0 then
  30.        NoZero(j) = myArray(i)
  31.        j=j+1
  32.     end if
  33. next
  34. 'here you put the nozero values to the second array
  35. End Sub
It's not a great code, but it'll give you an idea of how arrays, dim and redim works out.

Hope that helps
Jul 7 '07 #3
Killer42
8,435 Expert 8TB
It is actually possible to reference individual variables in more or less the way you suggested, using a function which accepts the variable name as a string. But it's a lot of work, for no benefit as far as I know. Much better to handle it with an array, as kadghar suggested.
Jul 9 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Mothra | last post: by
2 posts views Thread by John Wilkinson | last post: by
27 posts views Thread by Erik de Castro Lopo | last post: by
5 posts views Thread by Daz | last post: by
8 posts views Thread by mantrid | last post: by
53 posts views Thread by subramanian100in | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.