|
|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
- Sub ykcbf() '//2025.7.21 总表原格式拆分成多工作表
- Dim d As Object, ws As Workbook, sh As Worksheet, sht As Worksheet
- Dim arr As Variant, r As Long, c As Long, bt As Long, col As Long, i As Long
- Dim s As String, k As Variant, rng As Range, tm As Double
- Dim shtName As String
- tm = Timer
- Set d = CreateObject("scripting.dictionary")
- Set ws = ThisWorkbook
- Application.ScreenUpdating = False
- Application.DisplayAlerts = False
- Application.Calculation = xlCalculationManual
- On Error Resume Next
- Set sh = ws.ActiveSheet
- If sh Is Nothing Then
- MsgBox "未找到【Sheet1】工作表!", vbCritical
- GoTo CleanExit
- End If
- On Error GoTo 0
- bt = 2: col = 11
- With sh
- r = .Cells(.Rows.Count, col).End(xlUp).Row
- c = .Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
- arr = .Range(.Cells(1, 1), .Cells(r, c)).Value2
- End With
- For i = bt + 1 To UBound(arr)
- If Not IsEmpty(arr(i, col)) Then
- s = Trim(CStr(arr(i, col)))
- If Len(s) > 0 Then d(s) = 1
- End If
- Next
- Application.DisplayAlerts = False
- For Each sht In ws.Sheets
- If sht.Name <> sh.Name Then sht.Delete
- Next
- Application.DisplayAlerts = True
- For Each k In d.keys
- sh.Copy After:=ws.Sheets(ws.Sheets.Count)
- Set sht = ws.Sheets(ws.Sheets.Count)
- sht.Name = Left(Replace(k, "/", "_"), 31) ' 确保名称合法
- With sht
- On Error Resume Next
- .DrawingObjects.Delete
- .AutoFilterMode = False
- .Range("A1").Resize(r, c).AutoFilter col, "<>" & k
- Set rng = .AutoFilter.Range.Offset(bt).Resize(r - bt).SpecialCells(xlCellTypeVisible)
- If Not rng Is Nothing Then rng.EntireRow.Delete
- .AutoFilterMode = False
- On Error GoTo 0
- End With
- Next
- sh.Activate
- CleanExit:
- Application.ScreenUpdating = True
- Application.DisplayAlerts = True
- Application.Calculation = xlCalculationAutomatic
- MsgBox "■ 拆分操作完成 ■" & vbCrLf & _
- "═══════════════════════" & vbCrLf & _
- "■ 处理时间: " & Format(Timer - tm, "0.000") & "秒" & vbCrLf & _
- "■ 处理行数: " & UBound(arr) - bt & "行" & vbCrLf & _
- "■ 生成表数: " & d.Count & "个" & vbCrLf & _
- "═══════════════════════", _
- vbInformation, "执行报告"
- Set d = Nothing
- End Sub
复制代码
|
|