[英]Macro to count number of specific values/strings in column
I am trying to create a macro that will count specific values in a given column. 我正在尝试创建一个宏,该宏将计算给定列中的特定值。 For example, I want to count the number of all the cells in column G that contain "candy".
例如,我要计算G列中包含“ candy”的所有单元格的数量。 How would I go about doing this?
我将如何去做呢?
How about: 怎么样:
Sub dural()
Dim s As String
Dim r As Range
Dim wf As WorksheetFunction
Set wf = Application.WorksheetFunction
s = "candy"
Set r = Range("G:G")
MsgBox wf.CountIf(r, s)
'
' and if you want to count phrases including candy then:
'
s = "*" & s & "*"
MsgBox wf.CountIf(r, s)
End Sub
Edit : 编辑 :
and to store the result in a worksheet cell: 并将结果存储在工作表单元格中:
Sub dural()
Dim s As String
Dim r As Range
Dim wf As WorksheetFunction
Set wf = Application.WorksheetFunction
s = "candy"
Set r = Range("G:G")
s = "*" & s & "*"
[H3] = wf.CountIf(r, s)
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.