樓主如果只要獲得每一列符合條件的OK數(shù)和NG數(shù),其實函數(shù)公式就夠了,效率也很高。
示例1:在顯示OK的單元格里輸入 =COUNTIFS(B2:B21,">0",B2:B21,"<0.05");
示例2:在顯示NG的單元格里輸入 = 20 - OK的個數(shù),即可。
如果想實現(xiàn)截圖所示的效果:即用顏色區(qū)分開OK和NG屬性的單元格,同時計算個數(shù),則可以用VBA代碼來實現(xiàn)。
| Sub 計數(shù)并用顏色標(biāo)記() Dim rng As Range, c As Range, i%, ok%, ng% Dim startRow&, endRow& startRow = 2: endRow = 21 '在此處定義數(shù)據(jù)區(qū)域的開始行和結(jié)束行,根據(jù)樓主截圖,設(shè)定為2和21 i = 1 With 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 '在數(shù)據(jù)檢索區(qū)域的下一行寫入ok的個數(shù) .Cells(endRow + 2, i).Value = ng '在數(shù)據(jù)檢索區(qū)域的下兩行寫入ng的個數(shù) Loop Until i = 5 'i = 5意味著數(shù)據(jù)判斷要執(zhí)行完第E列再結(jié)束 End With End Sub |