|
借用office2008的代码,试试看是否合乎要求:
Sub yy()
Dim i As Integer, k As Integer, r As Integer, lc As Integer
Dim s As Integer, w As Integer, sh As Worksheet
Dim d As Object
Dim arr(), arr2(), brr
Set d = CreateObject("scripting.dictionary")
For Each sh In Sheets
If sh.Name <> "汇总" Then
With sh
lc = .Range("iv1").End(xlToLeft).Column
brr = .Range(.Cells(1, 1), .Cells(1, lc))
For i = 2 To lc - 1
d(brr(1, i)) = ""
Next i
End With
End If
Next sh
lc = d.Count + 2 '表头列数
Application.ScreenUpdating = False
'以下4行写表头,其中16及后面的17可以根据实际重新设置
Cells(3, 1) = "姓名": Cells(3, lc) = "合计"
Cells(3, 2).Resize(1, d.Count) = d.keys
Cells(16, 1) = "姓名": Cells(16, lc) = "合计"
Cells(16, 2).Resize(1, d.Count) = d.keys
d.RemoveAll
On Error Resume Next
Set d = CreateObject("scripting.dictionary")
With Sheets("汇总")
brr = .Range(.Cells(3, 1), .Cells(3, lc))
For i = 1 To lc
d(brr(1, i)) = i '记录汇总最大列数以及列名
Next
lc2 = .Range("iv1").End(xlToLeft).Column
brr = .Range(.Cells(1, 1), .Cells(1, lc2))
For i = 1 To lc2 - 1 Step 2
If brr(1, i + 1) = "√" Then
If Len(Sheets(brr(1, i)).Name) > 0 Then
If Err = 0 Then
With Sheets(brr(1, i))
r = .Range("a65536").End(xlUp).Row
If r > 1 Then
ar = .Range(.Cells(1, 1), .Cells(r, lc))
For j = 2 To r
k = k + 1
ReDim Preserve arr(1 To lc, 1 To k)
For w = 1 To lc
If ar(1, w) <> "" Then arr(d(ar(1, w)), k) = ar(j, w)
Next w
Next j
End If
End With
End If
End If
End If
Next i
.Range("a17").Resize(.UsedRange.Rows.Count, lc).Clear
.Range("a17").Resize(UBound(arr, 2), lc) = Application.Transpose(arr) '这一段是合并
.Range("a16").Resize(UBound(arr, 2) + 1, lc).Borders.LineStyle = xlContinuous
.Range("a16").Resize(1, lc).Interior.ColorIndex = 15
d.RemoveAll
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(12, lc).Clear '12根据实际情况设定
.Range("a4").Resize(UBound(arr2, 2), lc) = Application.Transpose(arr2) '这一段是汇总
.Range("a3").Resize(UBound(arr2, 2) + 1, lc).Borders.LineStyle = xlContinuous
.Range("a3").Resize(1, lc).Interior.ColorIndex = 15
End With
Application.ScreenUpdating = True
End Sub
求助.rar
(12.39 KB, 下载次数: 659)
|
|