使用如下的VBA代碼,您就可以在Excel中,統計某個字符或某個數字,甚至是某個字符串,在某個數據區域范圍內,所出現的次數,即出現幾次,或稱為有幾個。
代碼如下,是VBA宏代碼。
Set myb = CreateObject("scripting.dictionary"): myb("數字") = "次數"
Set rng = Application.InputBox("選擇要統計的區域:", Type:=8)
ActiveSheet.Cells.Interior.ColorIndex = 0
rng.Interior.ColorIndex = 3
For Each rng1 In rng
myb(rng1.Value) = Application.WorksheetFunction.CountIf(rng, rng1)
Next
Set rng3 = Application.InputBox("選擇結果輸出地:", Type:=8)
With rng3
.Resize(myb.Count) = Application.Transpose(myb.keys)
.Offset(, 1).Resize(myb.Count) = Application.Transpose(myb.items)
End With
Set myb = Nothing: Set rng3 = Nothing
End Sub