1.alt+F11
2.点击
3.拷贝代码
代码:
Sub NewSheets() Dim d As Object, sht As Worksheet, arr, brr, r, kr, i&, j&, k&, x& Dim Rng As Range, Rg As Range, tRow&, tCol&, aCol&, pd& Application.ScreenUpdating = False Application.DisplayAlerts = False Set d = CreateObject("scripting.dictionary") Set Rg = Application.InputBox("请您框选拆分依据列!只能选择单列单元格区域!", title:="提示", Type:=8) tCol = Rg.Column tRow = Val(Application.InputBox("请您输入总表标题行的行数?")) If tRow = 0 Then MsgBox "您未输入标题行行数,程序退出!": Exit Sub Set Rng = ActiveSheet.UsedRange arr = Rng tCol = tCol - Rng.Column + 1 aCol = UBound(arr, 2) For i = tRow + 1 To UBound(arr) If Not d.exists(arr(i, tCol)) Then d(arr(i, tCol)) = i Else d(arr(i, tCol)) = d(arr(i, tCol)) & "," & i End If Next For Each sht In Worksheets If d.exists(sht.Name) Then sht.Delete Next kr = d.keys For i = 0 To UBound(kr) If kr(i) <> "" Then r = Split(d(kr(i)), ",") ReDim brr(1 To UBound(r) + 1, 1 To aCol) k = 0 For x = 0 To UBound(r) k = k + 1 For j = 1 To aCol brr(k, j) = arr(r(x), j) Next Next With Worksheets.Add(, Sheets(Sheets.Count)) .Name = kr(i) .[a1].Resize(tRow, aCol) = arr .[a1].Offset(tRow, 0).Resize(k, aCol) = brr Rng.Copy .[a1].PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False .[a1].Select End With End If Next Sheets(1).Activate Set d = Nothing Erase arr: Erase brr MsgBox "数据拆分完成!" Application.ScreenUpdating = True Application.DisplayAlerts = True End Sub
4.保存
5.使用
参考资料:1.https://baijiahao.baidu.com/s?id=1638300768682628867&wfr=spider&for=pc