# Multiple looping structures- I'm desperate

#### VBA_Newbie

##### Active Member
Hi folks,

I'm trying to simplify this as much as possible because my posts tend to be tedious and confusing. Suppose I have 4 columns of data...A,B,C and D. I want to compare every possible pair of combinations...even ones that might not make sense (there is a reason, but I won't get into that). I was thinking of using multiple loops that look like this:

Outer loop: Control movement to the next row once all comparisons in the two inner loops are done.

Middle Loop: Control the number of times it completes the next loop (which will not always be based on 4 columns.

Inner Loop: Makes the paired comparison.

So visually it would like like this.

Outer Loop: Do for the first row.
Middle Loop Tells it to do 4 iterations of the inner loop, because there are 4 columns in this instance.

First iteration (Inner Loop)
A-A
A-B
A-C
A-D

Second iteration of inner loop
B-A
B-B
B-C
B-D (note the basis of comparison cell changes from A to B)

Third iteration of inner loop
C-A
C-B
C-C
C-D

....and fourth iteration
D-A
D-B
D-C
D-D

Back to outer Loop: Repeat for the second row.

If this makes any sense to anyone...my problem is that I can't come up with code that would do this. So if anyone can write it down logically for me...that would be VERY helpful...I could problably figure it out from there.

### Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

#### amigos

##### Active Member
not sure if that's what you need, but try belowe code and it may help you:

Sub LoopFour()

Dim i As Byte
Dim j As Byte
Dim k As Byte
Dim l As Byte

For i = 1 To 4
For j = 1 To 4
For k = 1 To 4
For l = 1 To 4

MsgBox i & j & k & l

Next l
Next k
Next j
Next i

End Sub

HTH

#### macleanb

##### Well-known Member
I'm not exactly sure what your after PLUS I am about to leave the office (I hope) The below code is a bit dumb in that it comapres every cell to every cell - when it should really realise it has done the comparison for previous cells already - but hopefully it will give you some ideas:

Code:
``````Private Sub CommandButton1_Click()
Dim MyCell1
Dim MyCell2
Dim MySearchRange

MySearchRange = Range("A1:z100")

For Each MyCell1 In MySearchRange
For Each MyCell2 In MySearchRange

If MyCell1 <> MyCell2 Then
' ?
End If

End If

Next MyCell2
Next MyCell1

End Sub``````

Replies
1
Views
200
Replies
6
Views
302
Replies
6
Views
175
Replies
0
Views
244
Replies
5
Views
796

1,195,590
Messages
6,010,611
Members
441,558
Latest member
lambierules

### 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.

### Which adblocker are you using?

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

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