求excel公式 一列数字里有正数和负数 我想要这一列连续出现正数最多的数目和他们的和 负数也是一样

问题描述:

求excel公式 一列数字里有正数和负数 我想要这一列连续出现正数最多的数目和他们的和 负数也是一样
就像这样
A列
-2
5
-5
-10
-5
-5
2
3
-5
-5
5
6
10
11
自己算的话
最多连续出现正数4个 32
最多连续出现负数4个 -25
要求:用数组公式或者是VBA直接算出以上结果 最主要的是能求出他们的和 也就是32和-25

给你做了一段宏命令,代码如下,拷进去试一下,在运行前,做好备份并确保欲计算的表格处于活动状态:Sub JianCe()Dim SUMz As DoubleDim SUMf As DoubleDim K As SingleDim KZmax As SingleDim KFmax As SingleDim KZ As...太感谢了,不过还需要对任意列进行运算,应该怎么弄啊,比如说F列如果要对任意列进行上述运算,需要调整代码段,只需要将代码里的相关列号,如代码cells(K,1)中的1调整为列号即可,不过可以在运行代码前,先选择一下欲排序的列,让代码自动获得这个参数。不知道你这样做是干嘛用?我为你调整一下代码,在运行下列代码前,请先点击一下欲排序列中的任意单元格。代码修正如下:Sub JianCe()Dim SUMz As DoubleDim SUMf As DoubleDim K As SingleDim KZmax As SingleDim KFmax As SingleDim KZ As SingleDim KF As SingleDim SUMzEND As SingleDim SUMfEND As SingleDim N As SingleN= ActiveCell.ColumnK = 2Do While ActiveSheet.Cells(K - 1, N) ""If Val(ActiveSheet.Cells(K - 1, N)) > 0 ThenSUMz = SUMz + Val(ActiveSheet.Cells(K - 1, N))KZ = KZ + 1ElseIf Val(ActiveSheet.Cells(K - 1, N)) KZmax ThenKZmax = KZSUMzEND = SUMzElseKZ = 0SUMz = 0End IfIf KF > KFmax ThenKFmax = KFSUMfEND = SUMfElseKF = 0SUMf = 0End IfEnd IfK = K + 1LoopActiveSheet.Cells(14, 2) = "连续正数个数"ActiveSheet.Cells(14, 3) = "连续正数合"ActiveSheet.Cells(14, 4) = "连续负数个数"ActiveSheet.Cells(14, 5) = "连续负数合"ActiveSheet.Cells(15, 2) = KZmaxActiveSheet.Cells(15, 3) = SUMzENDActiveSheet.Cells(15, 4) = KFmaxActiveSheet.Cells(15, 5) = SUMfENDEnd Sub注意:loop后面的语句是调整输出位置的语句,自己可以调整的。