Multiple looping structures- I'm desperate

VBA_Newbie

Active Member
Joined
Jan 7, 2005
Messages
258
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

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
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
 
Upvote 0
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.Address <> MyCell1.Address Then
        
            If MyCell1 <> MyCell2 Then
                ' ?
            End If
            
        End If


    Next MyCell2
Next MyCell1



End Sub
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,751
Members
448,989
Latest member
mariah3

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