1

I have a loop with several If statements in Excel VBA. This goes through and hides certain rows based on certain criteria. Basicially, if one of the statements is true then the row is hidden. Since only one of the statements has to be true for the row to be hidden it would be pointless for the rest of the statements to be tested once one of the statements is found to be true. How would I put in a line of code that would say to move onto the next iteration of the loop once the if statement is found to be true? Any help would be greatly appreciated!

For i = 1 To rng2.Rows.Count

    If Left(rng3.Cells(i, 1).Value, 8) = "CMS Part" Then
        If rng3.Cells(i, 1).Value <> "CMS Part D (CY " & Year(Date) & ")" Then
            rng3.Cells(i, 1).EntireRow.Hidden = True

        End If
    End If

    If rng4.Cells(i, 1).Value = "Yes" Then
        rng4.Cells(i, 1).EntireRow.Hidden = True

    End If

    If InStr(1, CStr(rng5.Cells(i, 1).Value), "test") > 0 Or InStr(1, CStr(rng5.Cells(i, 1).Value), "Test") > 0 Or InStr(1, CStr(rng5.Cells(i, 1).Value), "demo") > 0 Or InStr(1, CStr(rng5.Cells(i, 1).Value), "Demo") > 0 Or InStr(1, CStr(rng5.Cells(i, 1).Value), "TEST") > 0 Or InStr(1, CStr(rng5.Cells(i, 1).Value), "DO NOT USE") > 0 Then
        rng5.Cells(i, 1).EntireRow.Hidden = True

    End If

Next i
2
  • 2
    Use either a Select Case construct, or use an IF .... ElseIf .... ElseIf... End If construct Commented Mar 2, 2015 at 18:43
  • 1
    This is clearly a case for If...ElseIf; Select Case would be a bad choice here. Commented Mar 2, 2015 at 19:08

2 Answers 2

1

I know using goto statements is generally bad programming, but an option would be:

For i = 1 To rng2.Rows.Count

If Left(rng3.Cells(i, 1).Value, 8) = "CMS Part" Then
    If rng3.Cells(i, 1).Value <> "CMS Part D (CY " & Year(Date) & ")" Then
        rng3.Cells(i, 1).EntireRow.Hidden = True
        Goto Skip
    End If
End If

If rng4.Cells(i, 1).Value = "Yes" Then
    rng4.Cells(i, 1).EntireRow.Hidden = True
    Goto Skip
End If

If InStr(1, CStr(rng5.Cells(i, 1).Value), "test") > 0 Or InStr(1, CStr(rng5.Cells(i, 1).Value), "Test") > 0 Or InStr(1, CStr(rng5.Cells(i, 1).Value), "demo") > 0 Or InStr(1, CStr(rng5.Cells(i, 1).Value), "Demo") > 0 Or InStr(1, CStr(rng5.Cells(i, 1).Value), "TEST") > 0 Or InStr(1, CStr(rng5.Cells(i, 1).Value), "DO NOT USE") > 0 Then
    rng5.Cells(i, 1).EntireRow.Hidden = True
    Goto Skip
End If

Skip: Next i
Sign up to request clarification or add additional context in comments.

1 Comment

You can improve readability of this by using something like "continue_0" as label
0

I know others said it, but here is the code using elseif

For i = 1 To rng2.Rows.Count

    If Left(rng3.Cells(i, 1).Value, 8) = "CMS Part" Then
        If rng3.Cells(i, 1).Value <> "CMS Part D (CY " & Year(Date) & ")" Then
            rng3.Cells(i, 1).EntireRow.Hidden = True

        End If

    ElseIf rng4.Cells(i, 1).Value = "Yes" Then
        rng4.Cells(i, 1).EntireRow.Hidden = True

    ElseIf InStr(1, CStr(rng5.Cells(i, 1).Value), "test") > 0 Or InStr(1, CStr(rng5.Cells(i, 1).Value), "Test") > 0 Or InStr(1, CStr(rng5.Cells(i, 1).Value), "demo") > 0 Or InStr(1, CStr(rng5.Cells(i, 1).Value), "Demo") > 0 Or InStr(1, CStr(rng5.Cells(i, 1).Value), "TEST") > 0 Or InStr(1, CStr(rng5.Cells(i, 1).Value), "DO NOT USE") > 0 Then
        rng5.Cells(i, 1).EntireRow.Hidden = True

    End If

Next i

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.