The following code is supposed to hide a row based on two conditions. If CU3 equals "RSY" and CV3 equals "Y" then hide row 64. I don’t know how to add the second condition to make this code work. Also, I would like to modify the Range to (CU3 :CV & lastrow). Does anyone know how to correct it?
Thank you very much
For Each r In ws2.Range("CU3:CV5")
If r.Value = "RSU" And q.Value = "Y" Then (' I know the q variable is not working here, but I include it here to show you what I'm trying to do)
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
>Solution :
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