使用VBA基于相似的值对Excel行进行分组

我有report.xlsx文件,其中包含来自另外2个Excel工作簿的数据,这些工作簿是基于某个唯一值进行合并的。 Pool_RAM列具有来自file1.xlsx数据,而Pool_HDD列具有来自file2.xlsx数据。 这两列都包含一些重复的值。 现在我想根据相似的值对这些行进行分组,并按照下面提到的格式input。

Report.xlsx Actual Data

在这里输入图像描述

我希望数据以下面的格式。

Expected Format

在这里输入图像描述

请让我知道,如果你需要任何其他信息。

试试这个macros,并根据您的需要进行调整:

 Sub GroupMyData() 'Assuming the data source is in column F and G, from row 2 to 1000 'The result will be put in column K and L 'part1: filter to unique values, put in column J temporarily Range("F2:F1000").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("J2" _ ), Unique:=True 'part2: fill the data Range("K1") = Range("F1") Range("L1") = Range("G1") Range("J2").Select Range(Selection, Selection.End(xlDown)).Select n = Selection.Count Range("K2").Select ck = 2 cl = 2 For i = 1 To n txt = Range("J" & (i + 1)) numf = WorksheetFunction.CountIf(Range("F2:F1000"), txt) For j = 1 To numf Range("K" & ck) = txt ck = ck + 1 Next numg = WorksheetFunction.CountIf(Range("G2:G1000"), txt) For j = 1 To numg Range("L" & cl) = txt cl = cl + 1 Next If (ck > cl) Then cl = ck Else ck = cl End If ck = ck + 1 cl = cl + 1 Next 'part 3: delete the temporary data Range("J2:J1000").Select Selection.ClearContents Range("K2").Select End Sub