Sub text()
Dim d As Object, dic As Object, rng As Excel.Range, arr, brr(), crr, MyFile As String, a, i As Long, j As Long, k As Long, l, m, n, s As String, tmp, artmp
Set d = CreateObject("scripting.dictionary")
Set dic = CreateObject("scripting.dictionary")
arr = xlapp.Range("g5:ag" & xlapp.Range("g65536").End(xlUp).Row)
ReDim brr(1 To UBound(arr), 1 To 3)
For i = 1 To UBound(arr)
If Len(arr(i, 13)) Then
s = "(" & arr(i, 1) & "/" & Format(arr(i, 2), "0.00") & "*" & arr(i, 3) & ")"
If Not d.Exists(s) Then
d(s) = d.Count + 1
brr(d(s), 1) = s
End If
brr(d(s), 2) = brr(d(s), 2) + 1
dic(s & "|" & arr(i, 13)) = dic(s & "|" & arr(i, 13)) + 1
k = k + 1: l = l + arr(i, 25): n = n + arr(i, 27)
End If
j = j + 1
Next
For Each tmp In dic.keys
artmp = Split(tmp, "|")
brr(d(artmp(0)), 3) = brr(d(artmp(0)), 3) & "," & dic(tmp) & "|" & artmp(1)
Next
s = ""
For i = 1 To d.Count
If brr(i, 2) = Val(Mid(brr(i, 3), 2)) Then
s = s & ";" & brr(i, 2) & "|" & brr(i, 1) & Split(brr(i, 3), "|")(1)
Else
s = s & ";" & brr(i, 2) & "|" & brr(i, 1) & ":" & Mid(brr(i, 3), 2)
End If
Next
a = Mid(Replace(s, "|", "aaa"), 2)
d.RemoveAll
crr = xlapp.Range("AA5:AA" & xlapp.Range("g65536").End(xlUp).Row)
For i = 1 To UBound(crr)
If Len(crr(i, 1)) Then d(crr(i, 1)) = ""
Next
m = Join(d.keys, "/")
xlapp.DisplayAlerts = False
MyFile = Dir(xlapp.ActiveWorkbook.Path & "\*.xls")
xlapp.Workbooks.Open FileName:=xlapp.ActiveWorkbook.Path & "\" & MyFile
On Error Resume Next
Set rng = xlapp.InputBox("请选粘贴地址:", , "$E$25", Type:=8)
xlapp.Range(rng.Address) = IIf(Len(a) > 0, a & ";计" & Format(l, "0.00") & "。", "")
xlapp.Range(rng.Address).Offset(, -2) = j
xlapp.Range(rng.Address).Offset(, -1) = k
xlapp.Range(rng.Address).Offset(, 1) = l
xlapp.Range(rng.Address).Offset(, 2) = m
xlapp.Range(rng.Address).Offset(, 3) = n
xlapp.DisplayAlerts = True
End Sub