- 帖子
- 5923
- 主題
- 13
- 精華
- 1
- 積分
- 5986
- 點名
- 0
- 作業系統
- win10
- 軟體版本
- Office 2010
- 閱讀權限
- 150
- 性別
- 男
- 來自
- 台灣基隆
- 註冊時間
- 2010-5-1
- 最後登錄
- 2022-1-23
        
|
4#
發表於 2015-9-1 12:49
| 只看該作者
進階篩選+自動篩選- Option Explicit
- Sub Ex()
- Dim Sh As Worksheet, Ar As Variant, i As Integer, M As Variant
- With ActiveWorkbook.Sheets("彙總明細")
- .Range("b:b").AdvancedFilter xlFilterCopy, , .Cells(1, .Columns.Count), True
- With .Cells(1, .Columns.Count).EntireColumn
- Ar = .SpecialCells(xlCellTypeConstants)
- Ar = Application.WorksheetFunction.Transpose(Ar)
- .Cells = ""
- End With
- On Error GoTo Sheet_Add '處裡工作表不存在的錯誤
- For i = 2 To UBound(Ar)
- .Range("A1").AutoFilter Field:=2, Criteria1:=Ar(i)
- .UsedRange.Columns("a:d").Copy ActiveWorkbook.Sheets(Ar(i)).[a1] '"彙總明細" 自動篩選後的資料, 複製
- Next
- .Range("A1").AutoFilter '取消 "彙總明細"自動篩選模式
- .Activate
- On Error GoTo 0 '程式有錯誤不處裡
- '刪除工作表不存"彙總明細"篩選欄的類別
- Application.DisplayAlerts = False
- For Each Sh In ActiveWorkbook.Sheets
- If Sh.Name <> .Name Then If IsError(Application.Match(Sh.Name, Ar, 0)) Then Sh.Delete
- Next
- Application.DisplayAlerts = True
- End With
- Exit Sub
- '******************************
- Sheet_Add:
- ActiveWorkbook.Sheets.Add.Name = Ar(i)
- Resume
- End Sub
複製代碼 |
|