Sub 統計()
Dim n As Integer
n = WorksheetFunction.CountIf(Range("A:A"), "李強")
MsgBox "A列中李強的個數為:" & n
End Sub
以上為一個簡單的版本。 下面是一個不依賴工作表函式的版本,即可統計李強也可統計張三,並且會去掉首尾空格再比較的版本。這連個把按本都可以修改為Function。
Sub 統計2()
Dim i As Integer
Dim r As Integer, n As Integer
Dim str, rng
str = Application.InputBox(prompt:="請輸入要在A列中統計的姓名:", Default:="李強", Type:=2)
str = Trim(str)
r = Cells(Rows.Count, 1).End(xlUp).Row
rng = Range("A1:A" & r)
For i = 1 To r
If Trim(rng(i, 1)) = str Then n = n + 1
Next i
MsgBox "A列中" & str & "的個數為:" & n
Sub 統計()
Dim n As Integer
n = WorksheetFunction.CountIf(Range("A:A"), "李強")
MsgBox "A列中李強的個數為:" & n
End Sub
以上為一個簡單的版本。 下面是一個不依賴工作表函式的版本,即可統計李強也可統計張三,並且會去掉首尾空格再比較的版本。這連個把按本都可以修改為Function。
Sub 統計2()
Dim i As Integer
Dim r As Integer, n As Integer
Dim str, rng
str = Application.InputBox(prompt:="請輸入要在A列中統計的姓名:", Default:="李強", Type:=2)
str = Trim(str)
r = Cells(Rows.Count, 1).End(xlUp).Row
rng = Range("A1:A" & r)
For i = 1 To r
If Trim(rng(i, 1)) = str Then n = n + 1
Next i
MsgBox "A列中" & str & "的個數為:" & n
End Sub