突出显示具有重复金额的重复客户端。在excel中

时间:2014-05-09 17:45:46

标签: excel vba excel-vba

我正在尝试使用重复金额的VBA客户端突出显示并标记在excel中。例如:

Client   Amount
123      $5
123      $6
456      $5
456      $9
456      $9

我当前的代码会标记这样的示例:

Client   Amount    Check
123      $5        Duplicate
123      $6        OK
456      $5        Duplicate
456      $9        Duplicate
456      $9        Duplicate

实际上

Client   Amount 
123      $5
456      $5

应标记为“正常”,因为它不是同一客户端上的重复金额。

我的代码如下。

'Checks For Duplicates
k = 2
Set AccClm = Range("A2:A" & rowz) ' area to check '
Set AmtClm = Range("C2:C" & rowz)
iWarnColor = xlThemeColorAccent2

Do While ws.Cells(k, 1) <> ""
Acc = ws.Cells(k, 1)
Amt = ws.Cells(k, 3)
    If (WorksheetFunction.CountIf(AccClm, Acc) >= 2) And (WorksheetFunction.CountIf(AmtClm, Amt) >= 2) Then
                ws.Rows(k).Interior.ColorIndex = iWarnColor
                ws.Cells(k, 4) = "Duplicate"
            Else
                ws.Rows(k).Interior.Pattern = xlNone
    End If
    k = k + 1
Loop

我被困住了,任何想法都会受到赞赏。

1 个答案:

答案 0 :(得分:1)

使用COUNTIFS代替COUNTIF

        'Checks For Duplicates
    k = 2
    Set AccClm = Range("A2:A" & rowz) ' area to check '
    Set AmtClm = Range("C2:C" & rowz)
    iWarnColor = xlThemeColorAccent2

    Do While ws.Cells(k, 1) <> ""
    Acc = ws.Cells(k, 1)
    Amt = ws.Cells(k, 3)
        If WorksheetFunction.CountIfs(AccClm, Acc, AmtClm, Amt) >= 2 Then
                    ws.Rows(k).Interior.ColorIndex = iWarnColor
                    ws.Cells(k, 4) = "Duplicate"
                Else
                    ws.Rows(k).Interior.Pattern = xlNone
        End If
        k = k + 1
    Loop