|
|
- Sub ykcbf() ' 2025.12.12
- ApplicationSettings False
- col = Val(Application.InputBox("请输入拆分列列号:默认是1列", "拆分依据列列号", 1))
- bt = Val(Application.InputBox("请输入标题行数:默认是1行", "标题行数", 1))
- If col = 0 Or bt = 0 Then Exit Sub
- Set wb = ThisWorkbook
- Set sh = wb.Sheets(1)
- tm = Timer
- With sh
- If .AutoFilterMode Then .AutoFilterMode = False
- r = .Cells(.Rows.Count, col).End(xlUp).Row
- c = .Cells.Find("*", , xlValues, , xlByColumns, xlPrevious).Column
- Set rng = .[A1].Resize(r, c)
- arr = rng.Value
- End With
- Dim idx As Long
- If wb.Sheets.Count > 1 Then
- For idx = wb.Sheets.Count To 1 Step -1
- If wb.Sheets(idx).Name <> sh.Name Then
- wb.Sheets(idx).Delete
- End If
- Next idx
- End If
- Set d = CreateObject("Scripting.Dictionary")
- If col <= UBound(arr, 2) Then
- For i = bt + 1 To UBound(arr, 1)
- v = arr(i, col)
- If Len(Trim(CStr(v))) > 0 Then
- If Not d.Exists(v) Then d(v) = True
- End If
- Next i
- End If
- On Error Resume Next
- For Each k In d.Keys
- sh.Copy After:=wb.Sheets(wb.Sheets.Count)
- With wb.Sheets(wb.Sheets.Count)
- .Name = CleanSheetName(CStr(k))
- .DrawingObjects.Delete
- .UsedRange.Offset(bt).Clear
- rng.AutoFilter col, k
- rng.Offset(bt).Resize(rng.Rows.Count - bt).SpecialCells(xlCellTypeVisible).Copy .Range("A" & bt + 1)
- End With
- sh.AutoFilterMode = False
- Next k
- sh.Activate
- ApplicationSettings True
- If d.Count > 0 Then
- MsgBox "■ 拆分操作完成 ■" & vbCrLf & _
- "═══════════════════════" & vbCrLf & _
- "■ 处理时间: " & Format(Timer - tm, "0.000") & "秒" & vbCrLf & _
- "■ 处理行数: " & UBound(arr) - bt & "行" & vbCrLf & _
- "■ 生成表数: " & d.Count & "个" & vbCrLf & _
- "═══════════════════════", _
- vbInformation, "执行报告"
- End If
- End Sub
- Private Sub ApplicationSettings(ByVal Reset As Boolean)
- With Application
- .ScreenUpdating = Reset
- .DisplayAlerts = Reset
- .Calculation = IIf(Reset, xlCalculationAutomatic, xlCalculationManual)
- .AskToUpdateLinks = Reset
- .EnableEvents = Reset
- End With
- End Sub
-
- ' 清理非法工作表名称字符
- Function CleanSheetName(str As String) As String
- Dim illegalChars As String
- illegalChars = ":\/?*[]"
- For i = 1 To Len(illegalChars)
- str = Replace(str, Mid(illegalChars, i, 1), "_")
- Next i
- CleanSheetName = Left(Trim(str), 31) ' 限制长度
- End Function
复制代码
|
|