|
- Sub test0() '自动生成,数据格式一致,可纯用数组来写
-
- Dim ar(), br(), Dict As Object, Target As Range
- Dim i As Long, j As Long, k As Long, p As Long
- Dim m As Integer, Col As Long, sKey As String
-
- Set Target = Range("A1")
- Target.CurrentRegion.Clear
-
- Application.ScreenUpdating = False
-
- Set Dict = CreateObject("Scripting.Dictionary")
- ReDim ar(1 To Worksheets.Count - 1)
-
- Col = 1
- m = 7 '从 7月 始
- For i = m To 12 '到 12月 止
- If i = m Then
- For j = 1 To UBound(ar)
- ar(j) = Worksheets(j).Range("A1").CurrentRegion.Value
- If j = 1 Then ReDim br(1 To UBound(ar(j)) + 1, 1 To UBound(ar(j), 2) * 12)
- Next
- End If
- p = 2 + (i - m) * 3
- br(1, p) = i & "月"
- Target.Offset(, p - 1).Resize(, 3).HorizontalAlignment = xlCenterAcrossSelection
- For j = 1 To UBound(ar)
- Col = Col + 1
- br(2, Col) = i & Left(Worksheets(j).Name, 1)
- Dict.Add br(2, Col), Col
- Next
- Next
- p = 2 + (i - m) * 3
- br(1, p) = "合计"
- Target.Offset(, p - 1).Resize(, 3).HorizontalAlignment = xlCenterAcrossSelection
- For j = 1 To Worksheets.Count - 1
- Col = Col + 1
- Dict.Add Worksheets(j).Name & "年支出", Col
- br(2, Col) = Left(Worksheets(j).Name, 1) & "年支出"
- Next
-
- For k = 1 To UBound(ar)
- If k = 1 Then
- For i = 1 To UBound(ar(k))
- br(i + 1, 1) = ar(k)(i, 1)
- Next
- End If
- For j = 2 To UBound(ar(k), 2)
- If j < UBound(ar(k), 2) Then sKey = ar(k)(1, j) Else sKey = Worksheets(k).Name & ar(k)(1, j)
- p = Dict(sKey)
- If p Then
- For i = 2 To UBound(ar(k))
- br(i + 1, p) = ar(k)(i, j)
- Next
- End If
- Next
- Next
-
- With Target.Resize(UBound(br), Col)
- .Borders.LineStyle = xlContinuous
- With Intersect(.Offset(0), .Offset(1))
- .HorizontalAlignment = xlCenter
- .Font.Size = 10
- End With
- .Value = br
- .Cells(1).Resize(2).Merge
- End With
-
- Set Dict = Nothing
- Set Target = Nothing
- Application.ScreenUpdating = True
- Beep
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|