struggling with matching

shabbyporpoise

New Member
Joined
Jul 22, 2011
Messages
26
Hello forum
I am struggling with why I am getting "subscript out of range" error:confused:. My loop iterates the first time through for which ever value the user has selected for "num" once it gets to the end however I get the error. What I want to be happening is that every item is matched against the first item and then loops so that every item is matched against the second item and so on. Please help :help:
Code:
sub example()
(declared variables, and working array (rules()))
a=1
x=1
i=1
For x = 2 To num
        For i = 2 To num
        
                If rules(i, 3) = rules(i - a, 3) Then
                    MsgBox rules(i, 3) & "found a match" & rules(i - a, 3)
                    
                 Else
                    MsgBox rules(i, 3) & " no match  " & rules(i - a, 3)
            
                End If
               a = a + 1             
        Next i
        
      If Cells(i, 3) = "" Then
                MsgBox "next item"
               i = i + 1              
               End If
               
     
    Next x
   
End Sub
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Thanks for the reminder VoG
Code:
 Option Explicit Sub testArray()        Dim rules(1 To 10000, 1 To 6) As String    Dim intRow As Integer, intColumn As Integer, num As Integer    Dim i As Integer, a As Integer, x As Integer        num = InputBox("how many?")    i = 1    a = 1    x = 1        For intRow = 1 To num    For intColumn = 1 To 6         rules(intRow, intColumn) = Cells(intRow, intColumn).Value           Next intColumn     Next intRow         For x = 2 To num         For i = 2 To num                          If rules(i, 3) = rules(i - a, 3) Then                     MsgBox rules(i, 3) & "found a match" & rules(i - a, 3)                                       Else                     MsgBox rules(i, 3) & " no match  " & rules(i - a, 3)                              End If                a = a + 1                                                      Next i                If Cells(i, 3) = "" Then                 MsgBox "next item"                i = i + 1                End If                          Next x      End Sub
 
Upvote 0
I expect you need to move the a=1 line to follow the for x = ... line
 
Upvote 0
Question for you shg. do you mean just switch the lines where I initiate a=1 and x=1, or are you talking about during switching with x=2 to num? Thanks for you help :)
 
Upvote 0
I'm saying that the reason you're getting the subscript error is that you need to reinitialize variable a after the For x = 2 To num statement.
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,710
Members
452,939
Latest member
WCrawford

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