总结两个不同表格的两列

我真的需要你的帮助,我想总结列的标题的每一个外观不空的单元格。

例如:

|111|222|333|111|444|222|555 

 aaa |2 |2 |2 |2 |2 |2 |2 bbb | |2 |2 |2 |2 |2 |2 ccc | | |2 | |2 |2 | ddd |2 | | |2 | |2 | eee |2 | |2 | | | |2 

我想要的结果是这样的:

  |111|222|333|444|555 -------------------- |6 |6 |4 |3 |3 

有任何想法吗?

这工作。 您可能需要更新范围参考。 它在单独的工作表上输出摘要。

 Sub UserNameCount() Dim headers As Range, header As Range, countRng As Range, col As Long Set dict = CreateObject("Scripting.Dictionary") Set headers = Range("A1:G1") col = 1 For Each header In headers Set countRng = Range(header.Offset(1, 0), header.Offset(5, 0)) //update the '5' depending on the number of rows you have If Not dict.Exists(header.Value) Then dict.Add header.Value, WorksheetFunction.CountA(countRng) Else dict.Item(header.Value) = dict.Item(header.Value) + WorksheetFunction.CountA(countRng) End If Next header For Each v In dict.Keys With Worksheets("Sheet2") .Cells(1, col) = v .Cells(2, col) = dict.Item(v) col = col + 1 End With Next Set dict = Nothing End Sub 

在工作表的底部,使用COUNTA公式来计算每列的非空白。

  |111|222|333|111|444|222|555 ------------------------------------- aaa |2 |2 |2 |2 |2 |2 |2 bbb | |2 |2 |2 |2 |2 |2 ccc | | |2 | |2 |2 | ddd |2 | | |2 | |2 | eee |2 | |2 | | | |2 COUNTA |3 |2 |4 |3 |3 |4 |3 

粘贴特殊/转置标题行和COUNTA行到一个新的工作表。

  Header | COUNTA ----------------- 111 | 3 222 | 2 333 | 4 111 | 3 444 | 3 222 | 4 555 | 3 

现在创build一个数据透视表,列值为“Header”,然后将COUNTA公式相加得到结果。

如果您不想使用数据透视表,请将标题值粘贴到新工作表中,删除重复项,然后使用SUMIF公式获取每个标题值的总计。