Consulting

Results 1 to 3 of 3

Thread: Having trouble with a VBA double loop iteration

  1. #1

    Having trouble with a VBA double loop iteration

    Hi I'm trying to verify that all the records in one column are in the second column. Simple program. However, it never makes it past the point where the second column has a value that isn't in the first column. The point of this code is that the program skips to the next value when it can't find the current one anywhere in the first column. Here is the code, and thanks for any help.



    Private Sub RunMatch_Click()
    
    
    Dim maxRange, minRange As Integer
    Dim aVal, bVal As String
    minRange = CInt(minValue.Text)
    maxRange = CInt(noRecords.Text)
    
    
    For i = minRange To maxRange
        aVal = Cells(i, "A")
        bVal = Cells(i, "B")
        
        If aVal = bVal Then
            Cells(i, "C").Value = "1"
        Else
            For j = i + 1 To maxRange
            aVal = Cells(j, "A")
            If aVal = bVal Then
                Cells(i, "C").Value = "1"
                Exit For
            Else
                End If
            Next j
        
        End If
    Next i
    
    
    End Sub
    Last edited by evanpek87; 03-27-2015 at 11:21 AM. Reason: Added CODE tags

  2. #2
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    You could put
    =--(ISNUMBER(MATCH(B1,A:A,0))) in C1 and drag down

    Or perhaps
    =--(ISNUMBER(MATCH(B1,A1:A$65536,0)))

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    However, it never makes it past the point where the second column has a value that isn't in the first column. The point of this code is that the program skips to the next value when it can't find the current one anywhere in the first column. Here is the code, and thanks for any help.
    Q -- first or second column?

    Since your outer loop is the first column, I assume you meant when it cannot find a first column value in the second?

    This is not tested and I'm not 100% I understand, but it might give you a starting point


    '----- recommended
    Option Explicit
    
    Private Sub RunMatch_Click()
    
    '---- you need to repeat 'As Integer' otherwise they're Variant
    Dim maxRange As Integer, minRange As Integer
    Dim aVal As String, bVal As String
    Dim i As Long, j As Long
    
    minRange = CInt(minValue.Text)
    maxRange = CInt(noRecords.Text)
    
    With ActiveSheet
        For i = minRange To maxRange
           
            .Cells(i, 3).Value = 0
            
            For j = minRange To maxRange
                If .Cells(i, 1).Value = .Cells(i, 2).Value Then
                    .Cells(i, 3).Value = 1
                    Exit For
                End If
            Next j
        Next i
    End With
    End Sub

    There are more efficient ways, but this follows down your path
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •