macrosbuild筑设置总和,直到进入多个variables

我正在尝试创build一个Excelmacros,它会将正值时的值添加到variables“A”中。 当它变成负数时,将所有的负值加到“B”中。 当再次变为正值时,将所有值join“C”。 当它再次变为负数时,将所有的负数加到“D”…等等。

例如,如果我的数据是:数据 – Data-> (0,0,1,-1,1,1,-1,-1,0,0,-1)我希望输出是

 A=1 B=-1 C=2 D=-3 

我不确定如何去做。

假设你有一个数组中的值,这只是一个简单的事情,跟踪当前的标志,以确定何时需要切换:

 Private Sub DemoCode() Dim values() As Variant values = Array(0, 0, 1, -1, 1, 1, -1, -1, 0, 0, -1) Dim sign As Long, i As Long Dim current As Long, results As New Collection sign = 1 For i = LBound(values) To UBound(values) '0 is "signless". If values(i) <> 0 Then 'Did the sign change? If values(i) \ Abs(values(i)) <> sign Then 'Store the accumulated result. results.Add current 'Flip the sentinel sign. sign = -sign 'Reset the accumulator. current = 0 End If End If current = current + values(i) Next I 'Add the last result. results.Add current Dim result As Variant For Each result In results Debug.Print result Next End Sub