簡體   English   中英

宏以計算列中特定值/字符串的數量

[英]Macro to count number of specific values/strings in column

我正在嘗試創建一個宏,該宏將計算給定列中的特定值。 例如,我要計算G列中包含“ candy”的所有單元格的數量。 我將如何去做呢?

怎么樣:

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

編輯

並將結果存儲在工作表單元格中:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM