This Works。您可能需要更新范围参考。它在单独的工作表上输出摘要。
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
您的意思是计算非空单元格的数量? –
是的,vba代码很棒'这正是我需要的。 – Vera
如果它适合你,你能接受答案吗?它会帮助别人,如果他们正在寻找类似的问题 –