简体   繁体   English

移至excel vba中的下一个循环迭代

[英]move onto next iteration of loop in excel vba

I have a loop with several If statements in Excel VBA. 我在Excel VBA中有几个If语句的循环。 This goes through and hides certain rows based on certain criteria. 这会经历并根据某些条件隐藏某些行。 Basicially, if one of the statements is true then the row is hidden. 基本上,如果其中一个语句为true,则该行被隐藏。 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? 一旦发现if语句为真,我将如何放入一行代码,说明移至循环的下一个迭代? 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

I know using goto statements is generally bad programming, but an option would be: 我知道使用goto语句通常是不好的编程,但是可以选择:

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

I know others said it, but here is the code using elseif 我知道其他人说过,但这是使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM