在Excel中比较和计数连续的数据

我正在尝试使Excelmacros来比较和计数来自同一列的数据。

具体而言,我只想在单元格和单元格下方的单元格的绝对值都小于100的情况下进行计数。

最终目标:说下面的列是我拥有的数据,

241.197 96.747 88.325 156 53.666 55.372 -45.667 -207.152 

我希望macros返回值2.它需要计数[96.747 88.325]

[53.666 55.372 -45.667]

假设你有A1列的数据

 sub countTotal dim i as integer dim sum as double dim count as integer for i = 1 to Cells(Rows.Count, "A").End(xlUp).row sum = Abs(Range("A" & i).Value) + Abs(Range("A" & i + 1).Value) if sum < 100 then count = count + 1 next msgbox count end sub 

没有testing。 您应该看看它是否计算了列中的最后一个值+之后的空值,如果是,则将循环缩短1。

 Sub countTotal() Dim i As Integer Dim sum As Double Dim count As Integer For i = 1 To Cells(Rows.count, "A").End(xlUp).Row If Abs(Range("A" & i).Value) < 100 And Abs(Range("A" & i + 1).Value) < 100 Then count = count + 1 Else count = count End If Next MsgBox count End Sub 

根据你的代码,这是我想出来的。 如果数据如下所示,它会performance良好:

 120 70 -90 110 80 60 

在这种情况下macros返回2的值。

问题是我的一些数据看起来像这样:

 100 -80 90 70 -180 -190 

macros返回值为2,因为它计数[-80 90]和[90 70]。 但是我想这样做的是,当连续数据集的绝对值都小于100时,macros将其计为1.因此理想情况下,而不是返回2作为值,在这种情况下macros应该返回1。

你能指导我正确的方向吗? 谢谢!