这个VBA只在G列的值相同的情况下求和。我需要它来求和。
以下是原始数据,蓝色和红色项目将合并,并汇总总数
enter image description here
运行VBA后,我得到以下内容
enter image description here
所以蓝色的项目按预期工作,红色的项目没有。蓝色的项目加在一起,因为它们是相同的。无论如何都需要它们加在一起
下面是代码
Sub test()
Dim fn As String, a, x, s As String, i As Long, n As Long, temp
fn = Application.GetOpenFilename("CSVFiles,*.csv")
If fn = "False" Then Exit Sub
x = Split(CreateObject("Scripting.FileSystemObject").OpenTextFile(fn).ReadAll, vbNewLine)
ReDim a(1 To UBound(x) + 1, 1 To 1)
With CreateObject("Scripting.Dictionary")
For i = 0 To UBound(x)
If x(i) <> "" Then
s = Split(x(i), ",", 2)(1)
If Not .exists(s) Then
.Item(s) = .Count + 1
a(.Count, 1) = x(i)
Else
temp = Split(a(.Item(s), 1), ",")
temp(0) = temp(0) & Chr(2) & Split(x(i), ",")(0)
temp(6) = Val(temp(6)) + Val(Split(x(i), ",")(6))
a(.Item(s), 1) = Join(temp, ",")
End If
End If
Next
End With
With Cells(1).Resize(UBound(a, 1))
.CurrentRegion.ClearContents
.Value = a
.TextToColumns .Cells(1), 1, comma:=True
.Replace Chr(2), ", ", 2
.CurrentRegion.Columns.AutoFit
End With
End Sub
我一直试图修改VBA,但最终打破了它更多
1条答案
按热度按时间1aaf6o9v1#
将字典键限制为要聚合的4个字段。
CSV测试文件