VBA Excel,分组然后计数

时间:2017-11-09 23:45:01

标签: excel-vba vba excel

我不确定我是朝着正确的方向前进还是完全相反,但我希望通过Variant对数据进行分组,然后对其进行计数。例如,

    Dim Fruit As Variant
Fruit = Array("Apple", "Grape", "Lemon", "Melon", "Orange")

然后我想搜索/找到匹配“Fruit”的任何列,然后计算所有匹配。

在excel中,我专注于D栏

Example

1 个答案:

答案 0 :(得分:1)

可能有一种更有效的方法,但这有效......

Sub CountFruit()

    Dim Fruit As Variant
    Dim LR As Integer
    Dim t As Integer
    Dim g As Integer

    Fruit = Array("Apple", "Grape", "Lemon", "Melon", "Orange")

    LR = Cells(Rows.Count, 4).End(xlUp).Row

    t = 0

    For x = 2 To LR
        For g = LBound(Fruit) To UBound(Fruit)

            If Cells(x, 4).Value = Fruit(g) Then
                t = t + 1
            End If

        Next g
    Next x

    NumFruit = t

End Sub