For control variable error

awake2424

Registered User.
Local time
Yesterday, 18:41
Joined
Oct 31, 2007
Messages
479
Code:
Private Sub CommandButton3_Click()
    Dim rngCell As Range
    Dim i As Long
    Application.ScreenUpdating = False
    
'Coverage code
    i = Range("J" & Rows.Count).End(xlUp).Row
    For Each rngCell In Range("J2:E" & i)
        Select Case rngCell.Value
            Case Is <= 120
                rngCell.Interior.Color = RGB(255, 255, 0) 'Yellow
    [U]For Each rngCell In Range("D2:D" & i)[/U]
                rngCell.Interior.Color = RGB(255, 0, 0) 'Red
                        Case Is <= 0
        End Select
        End With
    Next rngCell
    
    'Reads code
    i = Range("H+I" & Rows.Count).End(xlUp).Row
    For Each rngCell In Range("H2:H + I2:I" & i)
        Select Case rngCell.Value
            Case Is <= 120
                rngCell.Interior.Color = RGB(255, 204, 0) 'Orange
        End Select
        End With
    Next rngCell
    
    
    ' Formula in column H
    Range("H2").Formula = "=G2/SUM($G$2:$G$200)"
    With Range("H2:H" & i)
        .FillDown
        .NumberFormat = "#.000"
    End With
    
    Application.ScreenUpdating = True
    

End Sub

Why doesn't access like the underlined (For control variable already in use). Thanks.
 
You forgot to close the previous For...Next clause:
Code:
'Coverage code
    i = Range("J" & Rows.Count).End(xlUp).Row
    For Each rngCell In Range("J2:E" & i)
        Select Case rngCell.Value
            Case Is <= 120
                rngCell.Interior.Color = RGB(255, 255, 0) 'Yellow
    [B][I][COLOR="Red"]Next rngCell[/COLOR][/I][/B]
    For Each rngCell In Range("D2:D" & i)
                rngCell.Interior.Color = RGB(255, 0, 0) 'Red
                        Case Is <= 0
        End Select
        End With
    Next rngCell
 
Code:
Private Sub CommandButton3_Click()
   Dim rngCell As Range
    Dim i As Long
    Application.ScreenUpdating = False
    
'Coverage code
    i = Range("J" & Rows.Count).End(xlUp).Row
    For Each rngCell In Range("J2:E" & i)
        Select Case rngCell.Value
            Case Is <= 120
                rngCell.Interior.Color = RGB(255, 255, 0) 'Yellow
    Next rngCell
    
    For Each rngCell In Range("D2:D" & i)
                rngCell.Interior.Color = RGB(255, 0, 0) 'Red
                        Case Is <= 0
        End Select
    Next rngCell

    'Reads code
    i = Range("H+I" & Rows.Count).End(xlUp).Row
    For Each rngCell In Range("H2:H + I2:I" & i)
        Select Case rngCell.Value
            Case Is <= 120
                rngCell.Interior.Color = RGB(255, 204, 0) 'Orange
        End Select
    Next rngCell
    
    
    ' Formula in column H
    Range("H2").Formula = "=G2/SUM($G$2:$G$200)"
    With Range("H2:H" & i)
        .FillDown
        .NumberFormat = "#.000"
    End With
    
    Application.ScreenUpdating = True
    

End Sub

Next without For error after adding the Next rngCell. Thanks.
 
Last edited by a moderator:
You've also intertwined For/Next blocks and the Select/Case block, which I'd be willing to bet is a problem.
 

Users who are viewing this thread

Back
Top Bottom