慕运维8079593
楼主如果只要获得每一列符合条件的OK数和NG数,其实函数公式就够了,效率也很高。示例1:在显示OK的单元格里输入 =COUNTIFS(B2:B21,">0",B2:B21,"<0.05");示例2:在显示NG的单元格里输入 = 20 - OK的个数,即可。如果想实现截图所示的效果:即用颜色区分开OK和NG属性的单元格,同时计算个数,则可以用VBA代码来实现。Sub 计数并用颜色标记()Dim rng As Range, c As Range, i%, ok%, ng%Dim startRow&, endRow&startRow = 2: endRow = 21 '在此处定义数据区域的开始行和结束行,根据楼主截图,设定为2和21i = 1With Sheet1 Do i = i + 1 Set rng = .Range(.Cells(startRow, i), .Cells(endRow, i)) ok = 0: ng = 0 For Each c In rng If c.Value > 0 And c.Value < 0.05 Then c.Interior.Color = RGB(255, 0, 0) '红色 ok = ok + 1 Else c.Interior.Color = RGB(0, 0, 255) '蓝色 ng = ng + 1 End If Next .Cells(endRow + 1, i).Value = ok '在数据检索区域的下一行写入ok的个数 .Cells(endRow + 2, i).Value = ng '在数据检索区域的下两行写入ng的个数 Loop Until i = 5 'i = 5意味着数据判断要执行完第E列再结束End WithEnd Sub