For Statement with Two Conditions

65 views Asked by At

I tried to combine all the codes together to make it look at one record at a time. The second part of the codes are looking at all the rows in a loop. Ideally, I would like to look at customer A's record and hide the row#66 or row#64. After that the code is supposed to start over from the top to look at Customer B,C,D... Thanks to the people who answered my questions about for loop. Thank you

Dim c As Range
Dim d As Range
With ThisWorkbook.Sheets("Template")
    .Rows.EntireRow.Hidden = False
    .Activate
    For Each c In .Range("E27,E32,E35,L36,L37,E39,E40,E41,E42,E43,E44,E45,E46,E49,E50,E51")
        If c.Value = 0 Then
            c.EntireRow.Hidden = True
        Else
            c.EntireRow.Hidden = False
        End If
    Next
    
   
End With



With Worksheets("Template")
    If .Range("E26").Value = 0 Or .Range("E27") = 0 Then
    Worksheets("Template").Rows("69").EntireRow.Hidden = True
    Else
    Worksheets("Template").Rows("69").EntireRow.Hidden = False
    End If
    If .Range("E32").Value = 0 Then
    Worksheets("Template").Rows("70").EntireRow.Hidden = True
    Else
    Worksheets("Template").Rows("70").EntireRow.Hidden = False
    End If

End With

Dim r As Range
Dim ws2 As Worksheet: Set ws2 = ThisWorkbook.Worksheets("Data File")
    For Each r In ws2.Range("CU3:CU5")
    If r.Value = "RSU" And r.Offset(0, 1).Value = "Y" Then
        Worksheets("Template").Rows("66").EntireRow.Hidden = False
        Worksheets("Template").Rows("64").EntireRow.Hidden = True
    Else
        Worksheets("Template").Rows("66").EntireRow.Hidden = True
        Worksheets("Template").Rows("64").EntireRow.Hidden = False
    End If
Next r
2

There are 2 answers

1
Tim Williams On BEST ANSWER

Here's one way you could do it, but I'm still confused how you account for multiple rows in the range you're looping over - you will be left with whatever the match works out as from the last row in that range.

Dim rw As Range, matched As Boolean
For Each rw In ws2.Range("CU3:CV5").Rows  'loop over rows in range
    'row matches conditions?
    matched = rw.Cells(1).Value = "RSU" And rw.Cells(2).Value = "Y"
    With Worksheets("Template")
        .Rows("66").EntireRow.Hidden = Not matched
        .Rows("64").EntireRow.Hidden = matched
    End With
Next rw

EDIT: after your update

Dim c As Range, ws As Range

Set ws = ThisWorkbook.Worksheets("Template")

ws.Rows.EntireRow.Hidden = False
For Each c In ws.Range("E27,E32,E35,L36,L37,E39,E40,E41,E42,E43,E44,E45,E46,E49,E50,E51").Cells
    If c.Value = 0 Then c.EntireRow.Hidden = c.Value = 0
Next
    
ws.Rows(69).EntireRow.Hidden = ws.Range("E26").Value = 0 Or ws.Range("E27") = 0
ws.Rows(70).EntireRow.Hidden = ws.Range("E32").Value = 0
1
taller On

This is what you are looking for. It's same as @Tim's comment. You don't have to keep Two Conditions in For clause.

Dim r As Range
For Each r In ws2.Range("CU3:CU5")
    If r.Value = "RSU" And r.Offset(0, 1).Value = "Y" Then
        Worksheets("Template").Rows("66").EntireRow.Hidden = False
        Worksheets("Template").Rows("64").EntireRow.Hidden = True
    Else
        Worksheets("Template").Rows("66").EntireRow.Hidden = True
        Worksheets("Template").Rows("64").EntireRow.Hidden = False
    End If
Next r

The code could be simplified as below.

    Dim r As Range, bFlag As Boolean
    For Each r In ws2.Range("CU3:CU5")
        bFlag = (r.Value = "RSU" And r.Offset(0, 1).Value = "Y")
        With Worksheets("Template")
            .Rows(66).EntireRow.Hidden = Not bFlag
            .Rows(64).EntireRow.Hidden = bFlag
        End With
    Next r