在“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
答案 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