|
- Sub splilt_sheet()
- Dim i%, irow%, icol%, split_col, aa, arr
- Dim sht0 As Worksheet, sht As Worksheet
- Dim d As Object, wb As Workbook
- Set wb = ActiveWorkbook
- Set sht0 = ActiveSheet
- Set d = CreateObject("scripting.dictionary")
- split_col = InputBox("请输入拆分的列数。例:(1,2,3……或a,b,c……)", , 6)
- If Not IsNumeric(split_col) Or split_col < 0 Then
- split_col = Application.Match(split_col, Array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j"), 0)
- If Application.IsNA(split_col) Then
- MsgBox "请正确输入拆分的列数。例:(1,2,3……或a,b,c……)"
- Exit Sub
- End If
- End If
- With sht0
- irow = .Cells(.Rows.Count, 1).End(xlUp).Row
- icol = .Cells(5, Columns.Count).End(xlToLeft).Column
- arr = .Range("a6:a" & irow).Offset(0, split_col - 1)
- For i = 1 To UBound(arr)
- If Not IsEmpty(arr(i, 1)) Then
- d(arr(i, 1)) = ""
- Else
- MsgBox "您要拆分的列有空白格,拆分后请人工核对对应数据"
- End If
- Next
- Application.DisplayAlerts = False
- On Error Resume Next
- For Each aa In d.keys
- If aa <> .Name Then
- Set sht = Sheets(aa)
- If Err Then
- Set sht = Sheets.Add(after:=Sheets(Sheets.Count))
- sht.Name = aa
- End If
- .Range("a5:z" & icol).AutoFilter field:=split_col, Criteria1:=sht.Name
- .Range("a1").Resize(irow, icol).Copy sht.Range("a1")
- sht.Columns("a:ac").AutoFit
- sht.Copy
- ActiveWorkbook.SaveAs Filename:=wb.Path & "" & aa & ".xlsx"
- ActiveWorkbook.Close
- End If
- Next
- On Error GoTo 0
- .Range("a1").AutoFilter
- .Select
- End With
- MsgBox "数据处理完毕"
- Application.DisplayAlerts = ture
- End Sub
复制代码 |
|