来自2个表的Excel Sumproduct,具有多个匹配条件

时间:2018-04-29 20:33:32

标签: excel excel-formula multiple-tables excel-match excel-tables

我需要帮助,

我有两个表:一个是员工在一个月(1月,2月等)处理项目/提案的日子,另一个是雇员费率。我需要多个匹配率和Jan天,其中所有标准在两个表中都相同,然后显示总和,如果Type:" Project"和年份" 2018"。我需要分别为每个月做这笔款项,但如果我找到了如何做一个,那么休息应该很简单:)[n / v]意味着没有价值。

表1

年|输入|项目|员工| Jan(天)|二月(天)

2018 |项目| Apple |约翰N | 6 | 7

2018 |项目| Apple | Alex T | [n / v] | 8

2017 |提案|香蕉|蒂姆C | 8 | [n / v]

2017 |提案|香蕉| Sena I | 9 | 6

2018 |项目|猕猴桃|约翰N | [n / v] | 6

2018 |项目|猕猴桃| Yen T | 4 | 5

表2

年|输入|项目|员工|率

2018 |项目| Apple |约翰N | 30

2018 |项目| Apple | Alex T | 40

2017 |提案|香蕉|蒂姆C | 20

2017 |提案|香蕉| Sena I | 30

2018 |项目|猕猴桃|约翰N | 10

2018 |项目|猕猴桃| Yen T | 40

如果我在同一张表中同时包含费率和天数,我可以使用SUMPRODUCT()执行此操作;

如果两列都在同一个表中,我使用的公式是:

= IFERROR(SUMPRODUCT(((表[年] = D2)(表[类型] ="项目")(表[Jan]> 0))* (表[Rate] * Table [Jan])),"")

但是有2个单独的表,它不会返回正确的值。

提前致谢

1 个答案:

答案 0 :(得分:0)

我的第一直觉是建议针对工作表编写SQL查询:

SELECT Table1.Year, Table1.Type, Table1.Project, Table1.Employee,
    SUM(Table1.[Jan(days)] * Table2.Rate) AS SumJan,
    SUM(Table1.[Feb(days)] * Table2.Rate) AS SumFeb,
    ...
FROM Table1
INNER JOIN Table2
    ON Table1.Year = Table2.Year
    AND Table1.Project = Table2.Project
    AND Table1.Employee = Table2.Employee
WHERE Table1.Year = 2018 AND Table1.Type = 'Project'
GROUP BY Table1.Year, Table1.Type, Table1.Project, Table1.Employee

但是,您使用的是Mac,因此using ADOCopyFromRecordset已关闭。也许这可以通过ODBC和Microsoft Query来实现。

或者,您可以将第二张表中的所有数据加载到Scripting.Dictionary(Mac的替代品可用here)。然后,你可以写一个像这样的函数:

Option Explicit

Dim dict As Dictionary

Sub FillDictionary()
    Set dict = New Dictionary

    ' load dictionary from table 2
    Dim rows As Variant
    rows = Table2.Range("A2").CurrentRegion.value
    Dim row As Variant, key As String
    For Each row In rows
        'assuming the first row has the column headers, we don't want to include those in the dictionary
        If row(0) <> "Year" Then
            key = Join(Array(row(0), row(1), row(2), row(3)), "_")
            dict(key) = row(4)
        End If
    Next
End Sub

Function GetSumRate(Year As Integer, RowType As String, Project As String, Employee As String, Days As Integer)
    If dict Is Nothing Then FillDictionary

    Dim key As String
    key = Join(Array(Year, RowType, Project, Employee), "_")
    If dict.Exists(key) Then GetSumRate = dict(key) * Days
End Function

然后,您可以从工作表单元格中调用该函数:

=GetSumRate(Table[Year], Table[Type], Table[Project], Table[Employee], Table[Jan])