|
- Sub test()
- Dim r%, i%
- Dim arr, brr()
- Dim wb As Workbook
- Dim ws As Worksheet
- Dim d As Object
- Dim rng As Range
- Application.ScreenUpdating = False
- Application.DisplayAlerts = False
- Set d = CreateObject("scripting.dictionary")
- For Each ws In Worksheets
- If Left(ws.Name, 1) Like "[A-D]" Then
- With ws
- r = .Cells(.Rows.Count, 2).End(xlUp).Row
- arr = .Range("b1:b" & r)
- m = 0
- For i = 1 To UBound(arr)
- If arr(i, 1) = "分行" Then
- m = m + 1
- r1 = .Cells(i, 2).MergeArea.Rows.Count + i
- c = .Rows(i & ":" & r1 - 1).Find(what:="*", LookIn:=xlValues, lookat:=xlWhole, searchorder:=xlByColumns, searchdirection:=xlPrevious).Column
- ReDim Preserve brr(1 To 4, 1 To m)
- brr(1, m) = i
- brr(2, m) = r1
- brr(3, m) = 0
- brr(4, m) = c
- End If
- Next
- ReDim crr(1 To UBound(brr, 2), 1 To UBound(brr))
- For i = 1 To UBound(brr)
- For j = 1 To UBound(brr, 2)
- crr(j, i) = brr(i, j)
- Next
- Next
-
- For i = 1 To UBound(crr) - 1
- crr(i, 3) = crr(i + 1, 1) - 2
- Next
- crr(UBound(crr), 3) = r
-
- For q = 1 To UBound(crr)
- For i = crr(q, 2) To crr(q, 3)
- If Not d.exists(arr(i, 1)) Then
- Set d(arr(i, 1)) = CreateObject("scripting.dictionary")
- End If
- If Not d(arr(i, 1)).exists(ws.Name) Then
- Set d(arr(i, 1))(ws.Name) = CreateObject("scripting.dictionary")
- End If
- If Not d(arr(i, 1))(ws.Name).exists(q) Then
- Set d(arr(i, 1))(ws.Name)(q) = .Cells(crr(q, 1) - 1, 2).Resize(crr(q, 2) - crr(q, 1) + 1, crr(q, 4) - 1)
- End If
- Set d(arr(i, 1))(ws.Name)(q) = Union(d(arr(i, 1))(ws.Name)(q), .Cells(i, 2).Resize(1, crr(q, 4) - 1))
- Next
- Next
- End With
- End If
- Next
- For Each aa In d.keys
- Application.SheetsInNewWorkbook = d(aa).Count
- Set wb = Workbooks.Add
- With wb
- m = 1
- For Each bb In d(aa).keys
- With Worksheets(m)
- .Name = bb
- For Each cc In d(aa)(bb).keys
- r = .Cells(.Rows.Count, 2).End(xlUp).Row
- If r > 1 Then
- r = r + 2
- End If
- d(aa)(bb)(cc).Copy .Cells(r, 2)
- Next
- End With
- m = m + 1
- Next
- ThisWorkbook.Worksheets("目录及报表说明").Copy before:=.Worksheets(1)
- .SaveAs Filename:=ThisWorkbook.Path & "" & "财务数据_" & aa & ".xls"
- .Close False
- End With
- Next
- Application.SheetsInNewWorkbook = 3
- End Sub
复制代码 |
|