MsgBox 没有给出值

MsgBox doesn’t give a value

在“V34:V99”这一列中,数字1,2,3,0的顺序是零散的。 我想计算有多少种情况会是 (n)(三个连续的 1 к=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

您不能以这种方式使用 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