如果excel中的项目代码相同,如何将两个表合并到一个表中并求和价格?

如果excel中的项目代码相同,如何将两个表合并到一个表中并求和价格?,excel,vba,group-by,sum,Excel,Vba,Group By,Sum,大家好, 我想把表1和表2合并成一个表(表3)。如果项目代码1和项目代码2相同,则将价格相加。最终输出应该类似于表3。我不确定这是否可以在没有VBA的情况下完成,最好不要使用VBA。但是,如果excel函数无法执行此分组任务,VBA仍然可以。任何建议都将不胜感激,谢谢 SimpleSUMIF()可以为您提供所需的输出。试一试- =SUMIF($B$4:$B$13,H4,$C$4:$C$13)+SUMIF($E$4:$E$13,H4,$F$4:$F$13) 请尝试下一个代码: Sub JoinT

大家好,

我想把表1和表2合并成一个表(表3)。如果项目代码1和项目代码2相同,则将价格相加。最终输出应该类似于表3。我不确定这是否可以在没有VBA的情况下完成,最好不要使用VBA。但是,如果excel函数无法执行此分组任务,VBA仍然可以。任何建议都将不胜感激,谢谢

Simple
SUMIF()
可以为您提供所需的输出。试一试-

=SUMIF($B$4:$B$13,H4,$C$4:$C$13)+SUMIF($E$4:$E$13,H4,$F$4:$F$13)

请尝试下一个代码:

Sub JoinTables()
  Dim sh As Worksheet, T1 As ListObject, T2 As ListObject, T3 As ListObject
  Dim arr1, arr2, arr3, arrHead, dict As Object, i As Long, iRow As Long, iCol As Long
  
  Set sh = ActiveSheet 'use here the necessary sheet
  Set T1 = sh.ListObjects("Table1") 'use here your first table name
  Set T2 = sh.ListObjects(2)        'use here your second table name
  
  arr1 = T1.DataBodyRange.Value     'put the data body range in an array
  arr2 = T2.DataBodyRange.Value
  arrHead = T1.HeaderRowRange.Value 'put thea header in an array
  
  Set dict = CreateObject("Scripting.Dictionary") 'create a dictionary
  For i = 1 To UBound(arr1)
    dict(arr1(i, 1)) = arr1(i, 2) 'input all in the dictionary
  Next i
  
  For i = 1 To UBound(arr2)  'process the second table, too
    If Not dict.Exists(arr2(i, 1)) Then
        dict(arr2(i, 1)) = arr2(i, 2) 'create a new key
    Else
        dict(arr2(i, 1)) = dict(arr2(i, 1)) + arr2(i, 2) 'add to the existing key
    End If
  Next i

  'interesting way of obtaining an array by other (one column) arrays combination:
  arr3 = Application.Transpose(Array(dict.Keys, dict.Items)) 'combine the two array !!!

  iRow = T1.HeaderRowRange.row 'the row where the new Table will be created
  iCol = T2.HeaderRowRange.Column + T2.HeaderRowRange.Columns.count 'column of the new table

  sh.Range(sh.cells(iRow, iCol + 1), sh.cells(iRow, iCol + 2)).Value = arrHead 'put the header
  sh.cells(iRow, iCol + 1).Offset(1).Resize(UBound(arr3), UBound(arr3, 2)).Value = arr3 'put the content
  sh.cells(iRow, iCol + 1).CurrentRegion.Select 'select it to become a table in the next code line

  Set T3 = sh.ListObjects.Add  'create the new table
  'Copy the second column format in the new created table
  T3.DataBodyRange.Columns(2).NumberFormat = T1.DataBodyRange.Columns(2).NumberFormat
End Sub
编辑以添加使用范围的版本:

Sub MergeRanges()
  Dim sh As Worksheet, lastR1 As Long, lastR2 As Long, firstCol1 As Long, firstCol2 As Long
  Dim arr1, arr2, arr3, arrHead, dict As Object, i As Long, iRow As Long, iCol As Long
  
  Set sh = ActiveSheet 'use here the necessary sheet
  firstCol1 = 7: firstCol2 = 10 'where are the first column of the two ranges
  iRow = 14                     ' the row where the header is
  
  lastR1 = sh.cells(rows.count, firstCol1).End(xlUp).row
  lastR2 = sh.cells(rows.count, firstCol2).End(xlUp).row
  
  arr1 = sh.Range(sh.cells(iRow + 1, firstCol1), sh.cells(lastR1, firstCol1 + 1)).Value 'put the range in an array
  arr2 = sh.Range(sh.cells(iRow + 1, firstCol2), sh.cells(lastR2, firstCol2 + 1)).Value
  arrHead = sh.Range(sh.cells(iRow, firstCol1), sh.cells(iRow, firstCol1 + 1)).Value  'put thea header in an array
  
  Set dict = CreateObject("Scripting.Dictionary") 'create a dictionary
  For i = 1 To UBound(arr1)
    dict(arr1(i, 1)) = arr1(i, 2) 'input all in the dictionary
  Next i
  
  For i = 1 To UBound(arr2)
    If Not dict.Exists(arr2(i, 1)) Then
        dict(arr2(i, 1)) = arr2(i, 2) 'create a new key
    Else
        dict(arr2(i, 1)) = dict(arr2(i, 1)) + arr2(i, 2) 'add to the existing key
    End If
  Next i
  
  arr3 = Application.Transpose(Array(dict.Keys, dict.Items)) 'combine the two array !!!

  iCol = firstCol2 + 3 'column of the new table
  
  sh.Range(sh.cells(iRow, iCol + 1), sh.cells(iRow, iCol + 2)).Value = arrHead 'put the header
  With sh.cells(iRow, iCol + 1).Offset(1).Resize(UBound(arr3), UBound(arr3, 2))
    .Value = arr3 'put the content
    .Columns(2).NumberFormat = sh.cells(iRow + 1, firstCol1 + 1).NumberFormat
  End With
End Sub

构建可伸缩解决方案的最简单方法是使用请测试我发布的代码。它根据上述逻辑在同一张表上创建一个新表,与第二张表相距一列,在第一张表的同一行上…您好,谢谢您的输入。如果我没有把问题说清楚,我很抱歉。我无法引用H列中的单元格,因为表3为空。因此,函数或VBA代码需要能够过滤表1和表2中的项目代码。想象一下,没有表3,我需要创建自己的表3Oh!然后你需要
VBA
powerquery
。谢谢你的输入,我复制了你的代码并在我的VBA编辑器中运行,它向我显示了“无效的外部过程”@weizer:你是在标准模块中复制的吗?错误出现在哪一行?您是否按照我的建议调整了代码以使用真实的表?我认为问题在于
sh.ListObjects(“表1”)
sh.ListObjects(2)
?因为我没有定义我的表,所以表的名称不起作用。我需要更改代码的哪一部分以适应我的情况?你的代码对我来说太高级和复杂了。@weizer:但这应该是显而易见的,我想…使用“表1”第一个表的名称,而在2上是第二个表的名称。我使用了(2),因为我无法将第二个表命名为“Table2”。在我的测试环境工作簿中存在这样一个名称alerady。我认为,表格命名为“Table1”和“Table2”并不是强制性的。如果活动工作表中只有两个表,则可以使用(1)和(2)…谢谢FaneDuru!它对我非常有效。对不起,我之前的问题不清楚。