VBA如何不计数行(或仅计数一次),如果它们具有相同的参数

时间:2018-07-04 15:50:40

标签: excel vba

我需要对“ rating_status2”列中的行计数为“ unclear”,并且如果它们具有相同的“ customer_nbr”仅行一次。怎么做?数据的结构如下:

rating_status2   customer_nbr
unclear            1         
unclear            2
approved           3
approved           4
unclear            1
unclear            1
unclear            2

由于“ customer_nbr”相同,此处“ unclear”值的行数应计算为2。

我的代码在下面,但是它仅用于计数“不清楚”的单元格。我该如何修改? `

Function Unclear_Rating()
Dim i As Integer
Dim intCounter(1) As Integer
Dim intLastRow As Integer
Dim zelle2 As Range
Dim posMonitoring2 As Integer
Dim zelle3 As Range
Dim posMonitoring3 As Integer

With Sheets("ICS Analysis")

    Set zelle2 = .Cells.Find("rating_status2", lookat:=xlWhole)
    posMonitoring2 = zelle2.Column
    Set zelle3 = .Cells.Find("customer_nbr", lookat:=xlWhole)
    posMonitoring3 = zelle3.Column

   intLastRow = .UsedRange.Rows.Count

    For i = 2 To intLastRow

            If .Cells(i, posMonitoring2).Value = "unclear" Then
                intCounter(1) = intCounter(1) + 1

            End If
    Next i

   Unclear_Rating = intCounter
    End With
End Function`

1 个答案:

答案 0 :(得分:0)

您可以在rating_status2上对数据进行排序,并使用小计(使用计数功能),这样,您甚至都不需要VBA。