Search code examples
vbaexcelexcel-2007

Too many iterations: syntax needed to highlight the cell row only after satifsying all criteria


I think I have an issue with the order of my For IF and Next statements, I am trying to only highlight the row where all conditions are meet, instead when my code makes it to the highlighting part all rows are individually highlighted and the code seems to run quite slow, I believe I am performing too many iterations?

Sub SWAPS100()

Dim rng As Range, lCount As Long, LastRow As Long
Dim cell As Object

Sheets("Output").Activate

With ActiveSheet

    LastRow = .Cells(Rows.Count, 1).End(xlUp).Row

    For Each cell In .Range("E2:E" & LastRow) 'new position
        If cell = "N" Then
        Debug.Print
                            For Each cell1 In .Range("U2:U" & LastRow) 'Secuirty type
                                If cell1 = "SW" Then
                                    For Each cell2 In .Range("J2:J" & LastRow) 'prior px
                                        If cell2 = 100 Then
                                            For Each cell3 In .Range("I2:I" & LastRow) 'current px
                                                    If cell3 <> 100 Then

            'With cell.Interior
        With cell.EntireRow.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 6382079
        .TintAndShade = 0
        .PatternTintAndShade = 0
        End With
                                                            End If
                                                        Next cell3
                                                        End If
                                                    Next cell2
                                                    End If
                                                Next cell1
                                                End If
                                            Next cell


End With

Solution

  • As @Raystafarian commented as I was typing, use And in your if statment instead of all the loops:

    Sub SWAPS100()
    
    Dim rng As Range, lCount As Long, LastRow As Long
    Dim cell As Object
    
    Sheets("Output").Activate
    
    With ActiveSheet
    
        LastRow = .Cells(Rows.Count, 1).End(xlUp).Row
    
        For Each cell In .Range("E2:E" & LastRow) 'new position
            If cell = "N" And cell.Offset(, 16) = "SW" And cell.Offset(, 5) = 100 _
                And cell.Offset(, 4) = 100 Then
                With cell.EntireRow.Interior
                    .Pattern = xlSolid
                    .PatternColorIndex = xlAutomatic
                    .Color = 6382079
                    .TintAndShade = 0
                    .PatternTintAndShade = 0
                End With
            End If
        Next cell
    
    
    End With
    

    With looping each row individually it will go slow and will most likely always justify. As long as you have one cell in each column that justifies the if statement then it will color all rows.

    Also this can be done with Conditional Formatting with the following formula:

    =AND($E2="N",$U2="SW",$J2=100,$I2=100)