VBA使用第二个标准计算唯一值

时间:2014-03-11 02:44:48

标签: excel vba excel-vba

我遇到了障碍,试图考虑如何编写VBA代码来计算唯一订单号的数量,以及每个销售人员的订单状态是打开还是关闭。

我正在编写会影响Sheet2的VBA。我需要VBA循环遍历Sheets(“Sheet2”)。Range(“A:A”)和(1)在Sheet1中查找名称; (2a)计算与销售人员姓名相对应的唯一订单号并且为“打开”,并且(2b)计算与销售人员姓名相对应并且为“已关闭”的唯一订单号。我为我想要用宏计算的值设计了“?”,并包含了答案数字。

非常感谢任何帮助。如果我能澄清任何事情,请告诉我。

Sheet1 - 订单

enter image description here

Sheet2 - 订单摘要

       A                   B                            C    
1  **Name**   **Count-Uniq Open Orders**   **Count-Uniq Closed Orders**
2   John              ? (answer: 2)                ? (answer: 0)
3   Ben               ? (answer: 1)                ? (answer: 1)
4   Fred              ? (answer: 1)                ? (answer: 0)       

4 个答案:

答案 0 :(得分:2)

测试:

Sub Tester()

Dim d1, d2, arrIn, r, tmp, nm, id, i
Dim c, k

    Set d1 = CreateObject("scripting.dictionary")
    Set d2 = CreateObject("scripting.dictionary")

    With Sheets("orders")
        arrIn = .Range(.Range("A2"), _
                       Cells(Rows.Count, 3).End(xlUp)).Value
    End With

    For r = 1 To UBound(arrIn, 1)
        nm = arrIn(r, 1) 'name
        id = arrIn(r, 2) 'order #
        If Not d1.exists(nm) Then
            d1.Add nm, Array(0, 0)
        End If
        If Not d2.exists(id) Then
            tmp = d1(nm)
            i = IIf(UCase(arrIn(r, 3)) = "OPEN", 0, 1)
            tmp(i) = tmp(i) + 1
            d1(nm) = tmp
            d2.Add id, 0
        End If
    Next r

    Set c = Sheets("summary").Range("a2")
    For Each k In d1.keys
        c.Resize(1, 3).Value = Array(k, d1(k)(0), d1(k)(1))
        Set c = c.Offset(1, 0)
    Next k
End Sub

答案 1 :(得分:1)

试试这个:)

Sub Macro1()

    Dim ws1 As Worksheet, ws2 As Worksheet, wsTemp As Worksheet
    Dim rng As Range
    Dim myformula1 As String, myformula2 As String

    With Application
        .DisplayAlerts = False
        .ScreenUpdating = False
    End With

    Set ws1 = ThisWorkbook.Sheets("Sheet1") 'where you have your Orders
    Set ws2 = ThisWorkbook.Sheets("Sheet2") 'where you have your summary which should have names in it
    ws1.Copy ThisWorkbook.Sheets(1)
    Set wsTemp = ActiveSheet: wsTemp.Name = "Temp"

    With wsTemp
        Set rng = .UsedRange
        rng.RemoveDuplicates Columns:=Array(1, 2, 3), Header:=xlYes
    End With

    myformula1 = "=COUNTIFS(" & wsTemp.Name & "!A:A,A2," & wsTemp.Name & "!C:C,""Open"")"
    myformula2 = "=COUNTIFS(" & wsTemp.Name & "!A:A,A2," & wsTemp.Name & "!C:C,""Closed"")"

    With ws2.Range(ws2.Range("A2"), ws2.Range("A" & ws2.Rows.Count).End(xlUp))
        .Offset(0, 1).Formula = myformula1
        .Offset(0, 2).Formula = myformula2
        .Offset(0, 1).Resize(, 2).Value = .Offset(0, 1).Resize(, 2).Value
    End With

    wsTemp.Delete
    With Application
        .DisplayAlerts = True
        .ScreenUpdating = True
    End With

End Sub

没有循环。只是给你一个选择:D
希望这会有所帮助。

答案 2 :(得分:0)

在表2的单元格B2中:

=SUM(IF(FREQUENCY(IF(Sheet1!$A$2:$A$10=$A2,IF(Sheet1!$C$2:$C$10=B$1,Sheet1!$B$2:$B$10)),IF(Sheet1!$A$2:$A$10=$A2,IF(Sheet1!$C$2:$C$10=B$1,Sheet1!$B$2:$B$10)))>0,1))

这是一个数组公式,以便确认暂停Ctrl + Shift并点击Enter,这会在开始和结束时添加{}分别

在单元格C3中:

=SUM(IF(FREQUENCY(IF(Sheet1!$A$2:$A$10=$A2,IF(Sheet1!$C$2:$C$10=C$1,Sheet1!$B$2:$B$10)),IF(Sheet1!$A$2:$A$10=$A2,IF(Sheet1!$C$2:$C$10=C$1,Sheet1!$B$2:$B$10)))>0,1))

同样,它是一个数组,所以Ctrl + Shift和Enter

然后将公式复制下来。

答案 3 :(得分:0)

作为替代方案,就数据透视表解决方案而言,以下答案涵盖了所需的技术:

Simple Pivot Table to Count Unique Values