|
回复 6楼 wenwen000424 的帖子
Sub yy()
Dim i As Integer, k As Integer, r As Integer
Dim s As Integer, w As Integer, sh As Worksheet
Dim d As Object
Dim arr(), arr2()
Set d = CreateObject("scripting.dictionary")
With Sheets("汇总")
For i = 1 To .Range("iv3").End(xlToLeft).Column
s = s + 1
d(.Cells(3, i).Value) = s '记录汇总最大列数以及列名
Next
For Each sh In Sheets
If sh.Name <> "汇总" Then
Set rng = .Range("a1:iv1").Find(sh.Name, , , 1)
If rng.Offset(, 1).Value = "√" Then
r = sh.Range("a65536").End(xlUp).Row
If r > 1 Then
ar = sh.Range("a1:f" & r) '下面有 If ar(1, w) <> "" Then 所以范围可以稍许大一点
For i = 2 To UBound(ar)
k = k + 1
ReDim Preserve arr(1 To s, 1 To k)
For w = 1 To UBound(ar, 2)
If ar(1, w) <> "" Then arr(d(ar(1, w)), k) = ar(i, w)
Next
Next
End If
End If
End If
Next
.Range("a17").Resize(UBound(arr, 2), 6) = Application.Transpose(arr) '这一段是合并
Set d = CreateObject("scripting.dictionary")
k = 0
For i = 1 To UBound(arr, 2)
If Not d.exists(arr(1, i)) Then
k = k + 1
d(arr(1, i)) = k
ReDim Preserve arr2(1 To UBound(arr), 1 To k)
For w = 1 To UBound(arr)
arr2(w, d(arr(1, i))) = arr(w, i)
Next
Else
For w = 2 To UBound(arr)
arr2(w, d(arr(1, i))) = arr2(w, d(arr(1, i))) + arr(w, i)
Next
End If
Next
.Range("a4").Resize(UBound(arr2, 2), 6) = Application.Transpose(arr2) '这一段是汇总
End With
End Sub |
|