Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

For Statement with Two Conditions

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading