MsgBox没有给出值

时间:2017-06-13 07:33:53

标签: vba excel-vba loops msgbox excel

在“V34:V99”列中,分散顺序中有数字1,2,3,0。 我想计算有多少情况(n)(连续三个к=3,不考虑零),如果发生2或3,则k=o。 我编写了代码,但它不起作用(没有给出很多情况(n))。

0
1
0
1
0
0
0
1
0
1
0
0
0
0
1
0
0
1
0
0
1
0
0
1
0
0
1
0
0
0
1
0
0
0
0
3
0
1
0
3
0
0
0
3
0
1
0
1
0
0
1
3
0
1
0
0
0
0
0
0
0
0
0
0
0
0

代码

Sub а33условие3()

    Dim k, n As Integer
    Dim parRange As Range

    Set parRange = Range("V34:V99")
    k = 0
    n = 0
    For Each Cell In parRange.Rows

        If Cell.Value = 1 Then
            k = k + 1
            If k = 3 Then
                n = n + 1
                k = o
                MsgBox n
           End If
        End If
        If Cell.Value = 2 Or 3 Then
            k = 0
        End If
    Next Cell  

End Sub

1 个答案:

答案 0 :(得分:0)

您不能以这种方式使用Or语句。您应该使用Cell.Value = 2 Or Cell.Value = 3此外,您可以保存If语句,请参阅下面的更新代码:

Sub а33условие3()

Dim k, n As Integer
Dim parRange As Range

Set parRange = Range("V34:V99")
k = 0
n = 0
For Each Cell In parRange.Rows

    If Cell.Value = 1 Then
        k = k + 1
        If k = 3 Then
            n = n + 1
            k = o
            MsgBox n
        End If
    ElseIf Cell.Value = 2 Or Cell.Value = 3 Then
        k = 0
    End If
Next Cell  

End Sub