在工作表中组合行和总和值

我有一个Excel表格(pipe道“|”来分隔列)的数据。

A|B|C|X|50|60 D|E|F|X|40|30 A|B|C|X|10|20 A|B|C|Y|20|20 A|B|C|X|20|70 D|E|F|X|10|50 A|B|C|Y|10|10 

我想要得到的结果是:

 A|B|C|X|80|150 A|B|C|Y|30|30 D|E|F|X|50|80 

值A,B,C和D,E,F就像唯一的标识符。 其实只有A或D可以考虑。 值X和Y就像“types”,整数是要求和的值。 这个样本被简化了,有数以千计的唯一标识符,十几种types和几十个值的总和。 行不sorting,types可以位于更高或更低的行中。 我试图避免使用数据透视表。

 Dim LastRow As Integer Dim LastCol As Integer Dim i As Integer LastCol = Sheets(1).Cells(1, Columns.Count).End(xlToLeft).Column LastRow = Sheets(1).Cells(Rows.Count, 1).End(xlUp).Row For i = 1 To LastRow ???? Next i 

上面的代码达到了循环遍历行的点,但我不清楚在那之后要做什么。

  1. 将它们排列在您认为重要的所有字母列上。
  2. 在右边未使用的列中,使用第二行中的以下公式,

    = IF($ A2&$ B2&$ C2&$ D2 = $ A3&$ B3&$ C3&$ D3,“”,SUMIFS(E:E,$ A:$ A,$ A2,$ B:$ B,$ B2,$ C :$ C,$ C2,$ D:$ D,$ D2))

  3. 只要将数据复制一列,然后将数据填入两列即可

  4. 过滤两列,删除空白。

    来自PRM-9000的辐射测量

  5. 可select将数据复制到新的报告工作表并删除E&F列。

附录:

使用某种forms的数组和一些简单的math运算可以实现更自动化的方法。 我已经select了一个字典对象,以便利用索引来识别前四个字母标识符中的模式。

要使用脚本字典,您需要进入VBE的工具►参考并添加Microsoft脚本运行时。 没有它,以下代码将不能编译。

以下内容已针对键和整数的dynamic列进行了调整。

 Sub rad_collection() Dim rw As Long, nc As Long, sTMP As String, v As Long, vTMP As Variant Dim i As Long, iNumKeys As Long, iNumInts As Long Dim dRADs As New Scripting.Dictionary dRADs.CompareMode = vbTextCompare iNumKeys = 5 'possibly calculated by num text (see below) iNumInts = 2 'possibly calculated by num ints (see below) With ThisWorkbook.Sheets("Sheet4").Cells(1, 1).CurrentRegion 'iNumKeys = Application.CountA(.Rows(2)) - Application.Count(.Rows(2)) 'alternate count of txts 'iNumInts = Application.Count(.Rows(2)) 'alternate count of ints For rw = 2 To .Cells(Rows.Count, 1).End(xlUp).row vTMP = .Cells(rw, 1).Resize(1, iNumKeys).Value2 sTMP = Join(Application.Index(vTMP, 1, 0), Chr(183)) If Not dRADs.Exists(sTMP) Then dRADs.Add Key:=sTMP, Item:=Join(Application.Index(.Cells(rw, iNumKeys + 1).Resize(1, iNumInts).Value2, 1, 0), Chr(183)) Else vTMP = Split(dRADs.Item(sTMP), Chr(183)) For v = LBound(vTMP) To UBound(vTMP) vTMP(v) = vTMP(v) + .Cells(rw, iNumKeys + 1 + v).Value2 Next v dRADs.Item(sTMP) = Join(vTMP, Chr(183)) End If Next rw rw = 1 nc = iNumKeys + iNumInts + 1 .Cells(rw, nc + 1).CurrentRegion.ClearContents 'clear previous .Cells(rw, nc + 1).Resize(1, nc - 1) = .Cells(rw, 1).Resize(1, nc - 1).Value2 For Each vTMP In dRADs.Keys 'Debug.Print vTMP & "|" & dRADs.Item(vTMP) rw = rw + 1 .Cells(rw, nc + 1).Resize(1, iNumKeys) = Split(vTMP, Chr(183)) .Cells(rw, nc + iNumKeys + 1).Resize(1, iNumInts) = Split(dRADs.Item(vTMP), Chr(183)) .Cells(rw, nc + iNumKeys + 1).Resize(1, iNumInts) = _ .Cells(rw, nc + iNumKeys + 1).Resize(1, iNumInts).Value2 Next vTMP End With dRADs.RemoveAll: Set dRADs = Nothing End Sub 

只要运行macros,针对您提供的样本数量。 我已经在第一行假设了某种forms的列标题标签。 词典对象被填充,并且在组合的标识符中的重复项将它们的数字求和。 剩下的就是把它们分开,并把它们返回到一个未使用区域的工作表中。

Rad测量收集

Microsoft脚本运行时的位置 – 在Visual Basic编辑器(又名VBE)中select工具►参考( Alt + TR ),向下滚动一半以上find它。

Microsoft脚本运行时