VBA how to loop variables?

yxz152830

Active Member
Joined
Oct 6, 2021
Messages
393
Office Version
  1. 365
Platform
  1. Windows
Gurus,
I'm trying to loop variables that have certain patterns but below code can't do it. How do I correct it? Thank you!
VBA Code:
Sub aisdjaidji()
ss1 = 1
ss2 = 3
ss3 = 10
ss4 = 9
ss5 = 5
For n = 1 To 5
Debug.Print "ss" & n
Next n
End Sub
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Try it like this:

VBA Code:
Sub aisdjaidji()
   
    Dim n As Long, ss(1 To 5) As Long
   
    ss(1) = 1
    ss(2) = 3
    ss(3) = 10
    ss(4) = 9
    ss(5) = 5
   
    For n = lbound(ss) to ubound(ss)
        Debug.Print ss(n)
    Next n

End Sub
 
Upvote 0
Try it like this:

VBA Code:
Sub aisdjaidji()
  
    Dim n As Long, ss(1 To 5) As Long
  
    ss(1) = 1
    ss(2) = 3
    ss(3) = 10
    ss(4) = 9
    ss(5) = 5
  
    For n = lbound(ss) to ubound(ss)
        Debug.Print ss(n)
    Next n

End Sub
what if I wanna assign n = 1 to 10 in between strings like this: ss (1 to 10) rate, ss (1 to 10) total?
 
Upvote 0
Can you provide an example, so we can understand what you mean?
basically I wanna set up variables with pattern like this ss01_rate, ss02_rate, ss03_rate so on and so forth so I can loop thru them for calculation. The changing part is right in the middle and im not sure how to use your method to create variables.
 
Upvote 0
Why do you need to call them ss01_rate, ss02_rate etc?

If these are VBA variables, the easiest approach would be to declare ss_rate(1 to 10) and loop through each ss( n).

Or perhaps these are Excel names? In which case you could loop:

VBA Code:
For n = 1 To 10
    'do stuff with ss01_rate etc, e.g.
    x = Range("ss" & Format(n, "00") & "_rate").Value
Next n
 
Upvote 0
Why do you need to call them ss01_rate, ss02_rate etc?

If these are VBA variables, the easiest approach would be to declare ss_rate(1 to 10) and loop through each ss( n).

Or perhaps these are Excel names? In which case you could loop:

VBA Code:
For n = 1 To 10
    'do stuff with ss01_rate etc, e.g.
    x = Range("ss" & Format(n, "00") & "_rate").Value
Next n
I decided to use dictionary method. Thanks anyways
 
Upvote 0
Gurus,
I'm trying to loop variables that have certain patterns but below code can't do it. How do I correct it? Thank you!
Short anwer: can not
alternative:
VBA Code:
Sub aisdjaidji()
ss1 = 1
ss2 = 3
ss3 = 10
ss4 = 9
ss5 = 5
For n = 1 To 5
    Debug.Print Array(ss1, ss2, ss3, ss4, ss5)(n - 1)
Next n
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,029
Messages
6,122,755
Members
449,094
Latest member
dsharae57

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top