Using an arry to index nested for loops

petereddy

New Member
Joined
Feb 6, 2014
Messages
43
When I use an array to index a set of nested for loops, I get an error ("For control variable already in use"). See this short script, for example:
Code:
Sub testArrayFor()
    Dim myArray(1)
    For myArray(1) = 1 To 2
        For myArray(0) = 3 To 4
            MsgBox myArray(1) & myArray(0)
        Next myArray(0)
    Next myArray(1)
End Sub
When I use do-until loops, it works, though. For example, this script works:
Code:
Sub testArrayDo()
    Dim myArray(1)
    myArray(1) = 1
    Do Until myArray(1) > 2
        myArray(0) = 3
        Do Until myArray(0) > 4
            MsgBox myArray(1) & myArray(0)
            myArray(0) = myArray(0) + 1
        Loop
        myArray(1) = myArray(1) + 1
    Loop
End Sub
I'm willing to use the do-until loops, but I'm wondering if I'm doing something wrong with the for loops. Any thoughts?
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Try this instead

Code:
Sub testArrayFor()
    Dim myArray(1), x As Integer, y As Integer
    For x = 1 To 2
        myArray(1) = x
            For y = 3 To 4
                myArray(0) = y
                MsgBox myArray(1) & myArray(0)
            Next y
    Next x
End Sub
 
Last edited:
Upvote 0
Hi Yongle,


Thanks for taking the time to review my question. Maybe my question was unclear. Is there a way to use an array to index a set of nested for loops? What I'm working on nests ten for loops, nine of which I'd like to index with the array, and then the tenth is going to call the index values.


If I do it your way and use i1, i2, etc., then in the tenth for loop, I'd need to say if j = 1, then i1, else, if j = 2 then i2, etc., where if I'm able to use an array, I can simply call myArray(j).


I'm moving ahead with do-until loops.


Peter
 
Upvote 0
This may help


Code:
Sub Arrays()
    Const s = "_"
    Dim arrA, arrB, arrC, A, B, C
    Dim myStr As String
    
    arrA = Array(1, 2, 3, 4, 5)
    arrB = Array("A", "B", "C")
    arrC = Array("Cat", "Dog", "Cow")
    
    For Each A In arrA
        For Each B In arrB
            For Each C In arrC
                myStr = myStr & A & s & B & s & C & vbCr
            Next C
        Next B
    Next A
    MsgBox myStr
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,614
Messages
6,120,530
Members
448,969
Latest member
mirek8991

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