【问题标题】:Consolidate data and provide average of the consolidated data合并数据并提供合并数据的平均值
【发布时间】:2017-09-01 02:14:11
【问题描述】:

我正在编写一个宏,它将用于合并来自一系列单元格的数据。我有一个表格,其中包含要在 Range D2:J6 中的 sheet("1") 中合并的数据,并且目标位置再次在 M2:R2 中的 sheet("1") 中(列 M 到 R,但它们包含标题)。我已经编写了下面的部分代码,它适用于它们并为它们运行。但是,即使它没有说它有错误,它也不会正确运行..宏运行后我正在从我的 excel 中截取屏幕截图..

正如您从图像中看到的那样,我想合并 D 行中的重复值,并将位于 E、F、G、H、I、J 列中的值的平均值打印在与合并值相同的行上在 D 列中。例如对于 D 列中的值“Gebze 6832”,我想将其作为重复项删除,使其成为目标中的一个单元格并打印 E、F、G、H、I 列的平均值, J 来自在目标列中合并到它旁边的两行。

我的代码在下面(更新)

Dim ws As Worksheet
Dim dataRng As Range
Dim dic As Variant, arr As Variant
Dim cnt As Long
Set ws = Sheets("1")
With ws
lastrow = .Cells(.Rows.Count, "D").End(xlUp).Row    'get last row in Column D
Set dataRng = .Range("D2:D" & lastrow)              'range for Column D
Set dic = CreateObject("Scripting.Dictionary")
arr = dataRng.Value
For i = 1 To UBound(arr)
dic(arr(i, 1)) = dic(arr(i, 1)) + 1
Next
.Range("M2").Resize(dic.Count, 1) = Application.WorksheetFunction.Transpose(dic.keys)   'uniques data from Column D
.Range("Q2").Resize(dic.Count, 1) = Application.WorksheetFunction.Transpose(dic.items)
cnt = dic.Count
For i = 2 To cnt + 1
.Range("N" & i & ":P" & i).Formula = "=SUMIF($D$2:$D$" & lastrow & ",$M" & i & ",E$2:E$" & lastrow & ")/" & dic(.Range("M" & i).Value)
.Range("R" & i).Formula = "=IF(INDEX($I$2:$I$" & lastrow & ",MATCH($M" & i & ",$D$2:$D$" & lastrow & ",0))=0,N" & i & ","""")"
.Range("S" & i).Formula = "=IF(INDEX($I$2:$I$" & lastrow & ",MATCH($M" & i & ",$D$2:$D$" & lastrow & ",0))=0,Q" & i & ","""")"
.Range("T" & i).Formula = "=IF($S" & i & ">0,SUMPRODUCT(($D$2:$D$" & lastrow & "=$M" & i & ")*(($J$2:$J$" & lastrow & "-$E$2:$E$" & lastrow & ")>3%)),"""")"
Next i
.Range("M" & i).Value = "Grand Total"
.Range("N" & i & ":P" & i).Formula = "=AVERAGE(N2:N" & cnt + 1 & ")"
.Range("Q" & i).Formula = "=SUM(Q2:Q" & cnt + 1 & ")"
.Range("R" & i).Formula = "=AVERAGE(R2:R" & cnt + 1 & ")"
.Range("S" & i & ":T" & i).Formula = "=SUM(S2:S" & cnt + 1 & ")"
.Range("N2:T" & .Cells(.Rows.Count, "M").End(xlUp).Row + 1).Value = .Range("N2:T" & .Cells(.Rows.Count, "M").End(xlUp).Row + 1).Value
End With

【问题讨论】:

  • 当然,我无法将您的屏幕截图复制到工作表中进行测试,但我认为数据透视表会完全按照您的描述进行。
  • 好吧,我用 excel vba 弄明白了……我的错误是我没有 LeftColumn:=True 行……顺便谢谢你的建议……

标签: vba excel consolidation


【解决方案1】:

假设您的数据在从Row 2 开始的Column D to Column J 范围内,并且输出必须从Column M to Column SRow 2 显示,以下可能会有所帮助。

Sub Demo()
    Dim ws As Worksheet
    Dim dataRng As Range
    Dim dic As Variant, arr As Variant
    Dim cnt As Long

    Set ws = ThisWorkbook.Sheets("Sheet4")  'change Sheet4 to your data sheet

    Application.ScreenUpdating = False
    With ws
        lastRow = .Cells(.Rows.Count, "D").End(xlUp).Row    'get last row in Column D
        Set dataRng = .Range("D2:D" & lastRow)              'range for Column D
        Set dic = CreateObject("Scripting.Dictionary")
        arr = dataRng.Value

        For i = 1 To UBound(arr)
            dic(arr(i, 1)) = dic(arr(i, 1)) + 1
        Next
        .Range("M2").Resize(dic.Count, 1) = Application.WorksheetFunction.Transpose(dic.keys)   'uniques data from Column D
        cnt = dic.Count
        For i = 2 To cnt + 1
            .Range("N" & i & ":S" & i).Formula = "=SUMIF($D$2:$D$" & lastRow & ",$M" & i & ",E$2:E$" & lastRow & ")/" & dic(.Range("M" & i).Value)
        Next i
        .Range("N2:S" & .Cells(.Rows.Count, "M").End(xlUp).Row).Value = .Range("N2:S" & .Cells(.Rows.Count, "M").End(xlUp).Row).Value
    End With
    Application.ScreenUpdating = True
End Sub

此代码将给出以下结果。

【讨论】:

  • 哇,答案太棒了!既然你写得如此分析,你认为有可能实现三个小改动吗?第一个是合并到 G 列(最多到 Palls),因此只占用目标表中的单元格 M、N、O、P ......第二个是计算每行合并的行数和将它们粘贴到 Q 列(例如,对于 Gebze 6832 的编号为 2,对于 Atena 8860 的编号为 1 等)
  • @PericlesFaliagas - 是的,给我一些时间。
  • 第三个是最困难的一个,能够以一种我不确定是否可能的方式,仅针对源行合并目标表的 N 列中的结果第 I 列有 0 的表...
  • @PericlesFaliagas - 我已经回答了你的另一个问题here。你能展示一下source table that have 0 in column I 条件下的输出吗?
  • @PericlesFaliagas - 在.Range("T" & i).Formula 中将IF($S" & i & ">0, 更改为IF(ISNUMBER($S" & i & "),
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-03
  • 1970-01-01
  • 2013-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多