我试图遍历三个数据列表并隐藏总值为0的行。尽管我不断收到“Loop Without Do”错误,但是有人可以发现我做错了什么。

干杯。

Sub Button1_Click()

Dim Brow1 As Integer
Dim Brow2 As Integer
Dim Brow3 As Integer

Dim Trow1 As Integer
Dim Trow2 As Integer
Dim Trow3 As Integer

Brow1 = 62
Trow1 = 3
Brow2 = 126
Trow2 = 67
Brow3 = 190
Trow3 = 131

Do While Brow1 > Trow1

    If Range("P" & Brow1).Value = 0 Then
        Rows(Brow1).EntireRow.Hidden = True

    ElseIf Range("P" & Brow1).Value <> 0 Then

       Brow1 = Brow1 - 1
Loop


Do While Brow2 > Trow2

    If Range("P" & Brow2).Value = 0 Then
        Rows(Brow2).EntireRow.Hidden = True

    ElseIf Range("P" & Brow2).Value <> 0 Then
       Brow2 = Brow2 - 1
Loop

Do While Brow3 > Trow3

    If Range("P" & Brow3).Value = 0 Then
        Rows(Brow3).EntireRow.Hidden = True

    ElseIf Range("P" & Brow3).Value <> 0 Then
       Brow3 = Brow3 - 1
Loop

End Sub

最佳答案

您的If语句均未正确关闭。在每个“ElseIF”之后添加“End If”,您应该会得到预期的行为:

Do While Brow1 > Trow1

    If Range("P" & Brow1).Value = 0 Then
        Rows(Brow1).EntireRow.Hidden = True

    ElseIf Range("P" & Brow1).Value <> 0 Then

       Brow1 = Brow1 - 1
    End If
Loop

10-02 10:38