|
- Sub 工作表拆分()
- Application.ScreenUpdating = False
- Application.DisplayAlerts = False
- Dim arr As Variant
- Dim i, s As Integer
- Dim brr()
- Dim wb, wb1 As Workbook
- Dim d As Object
- Dim hlrow As Integer, cutcolumn As Integer, cuttype As Integer
- Set d = CreateObject("scripting.dictionary")
- Dim sh As Worksheet
- hlrow = InputBox("请输入标题最末行数:", "拆分表格", "1")
- bwhs = InputBox("请输入表尾的总行数:", "拆分表格", "0")
- cutcolumn = InputBox("请输入拆分依据列(第一列是1,第二列是2,以此类推):", "拆分表格", "1")
- cuttype = InputBox("请选择拆分类型(拆分到本工作簿是1,拆分为多个独立工作簿是2,拆分为一个工作簿是3):", "拆分表格", "1")
- bb = MsgBox("是否去除表尾 是为去除 否为保留表尾", vbYesNo)
-
- lastCol = Selection.SpecialCells(xlCellTypeLastCell).Column
- lastRow = Selection.SpecialCells(xlCellTypeLastCell).Row
- arr = ActiveSheet.Range("a1", ActiveSheet.Cells(lastRow - bwhs, lastCol))
- For i = hlrow + 1 To UBound(arr)
- If Not d.exists(arr(i, cutcolumn)) Then
- If Cells(i, cutcolumn) = "" Then GoTo 3
- Set d(arr(i, cutcolumn)) = ActiveSheet.Range("a" & i).Resize(1, UBound(arr, 2))
- Else
- Set d(arr(i, cutcolumn)) = Union(d(arr(i, cutcolumn)), ActiveSheet.Range("a" & i).Resize(1, UBound(arr, 2)))
- End If
- 3:
- Next i
- x = d.keys
- If cuttype = 1 Then
- For k = 0 To d.Count - 1
-
-
- Worksheets.Add after:=Worksheets(Worksheets.Count)
-
- ActiveSheet.Name = x(k)
- ThisWorkbook.Worksheets(1).Rows("1:" & hlrow).Copy ActiveSheet.[a1]
- d.items()(k).Copy ActiveSheet.Cells(hlrow + 1, 1)
- If bb = 7 Then
- zdRow = Selection.SpecialCells(xlCellTypeLastCell).Row
- qsh = lastRow - Val(bwhs) + 1
- ThisWorkbook.Worksheets(1).Rows(qsh & ":" & lastRow + 1).Copy ActiveSheet.Cells(zdRow + 1, 1)
- Else
- End If
- Next k
- End If
- If cuttype = 3 Then
- Application.SheetsInNewWorkbook = d.Count
- Set wb1 = Workbooks.Add
- i = 1
- For Each k In d.keys
- wb1.Worksheets(i).Name = k
- wb1.Worksheets(i).Activate
- ThisWorkbook.Worksheets(1).Rows("1:" & hlrow).Copy wb1.Worksheets(x(k1)).[a1]
- d.items()(k1).Copy wb1.Worksheets(x(k1)).Cells(hlrow + 1, 1)
- If bb = 7 Then
- zdRow = Selection.SpecialCells(xlCellTypeLastCell).Row
- qsh = lastRow - Val(bwhs) + 1
- ThisWorkbook.Worksheets(1).Rows(qsh & ":" & lastRow + 1).Copy ActiveSheet.Cells(zdRow + 1, 1)
- Else
- End If
- k1 = k1 + 1
- i = i + 1
- Next k
- End If
-
- For k = 0 To UBound(x)
-
- If cuttype = 2 Then
- Application.SheetsInNewWorkbook = 1
- Set wb = Workbooks.Add
- With wb.Worksheets(1)
- ThisWorkbook.Worksheets(1).Rows("1:" & hlrow).Copy .[a1]
- d.items()(k).Copy .Cells(hlrow + 1, 1)
- If bb = 7 Then
- zdRow = Selection.SpecialCells(xlCellTypeLastCell).Row
- qsh = lastRow - Val(bwhs) + 1
- ThisWorkbook.Worksheets(1).Rows(qsh & ":" & lastRow + 1).Copy ActiveSheet.Cells(zdRow + 1, 1)
- Else
- End If
- wb.SaveAs Filename:=ThisWorkbook.Path & "" & x(k) & ".xls"
- wb.Close
- End With
- End If
- Next k
- If cuttype = 3 Then
- wb1.SaveAs Filename:=ThisWorkbook.Path & "" & "拆分数据表.xls"
- wb1.Close False
- End If
- 5:
- Application.DisplayAlerts = True
- Application.ScreenUpdating = True
- MsgBox "拆分完毕!"
-
- End Sub
复制代码 |
|