用VBA写程序实现提取合计A列相同项在C列数量并生成新表
时间: 2023-03-26 22:01:03 浏览: 119
您好,我可以回答这个问题。您可以使用VBA编写程序来实现提取合计A列相同项在C列数量并生成新表。具体实现方法可以参考以下代码:
Sub CountAndGenerateNewTable()
Dim lastRow As Long
Dim i As Long
Dim dict As Object
Dim key As Variant
Dim count As Long
Dim newTable As Worksheet
'获取当前工作表的最后一行
lastRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
'创建字典对象
Set dict = CreateObject("Scripting.Dictionary")
'遍历A列,统计相同项在C列的数量
For i = 2 To lastRow
If dict.Exists(ActiveSheet.Cells(i, "A").Value) Then
dict(ActiveSheet.Cells(i, "A").Value) = dict(ActiveSheet.Cells(i, "A").Value) + ActiveSheet.Cells(i, "C").Value
Else
dict.Add ActiveSheet.Cells(i, "A").Value, ActiveSheet.Cells(i, "C").Value
End If
Next i
'创建新表
Set newTable = Worksheets.Add
'将统计结果写入新表
i = 1
For Each key In dict
newTable.Cells(i, "A").Value = key
newTable.Cells(i, "B").Value = dict(key)
i = i + 1
Next key
End Sub
希望能对您有所帮助。
阅读全文