標題:
[發問]
篩選完後,分別放置不同工作表
[打印本頁]
作者:
jj369963
時間:
2014-11-30 22:57
標題:
篩選完後,分別放置不同工作表
Dear 大大們:
小問題
1.想以class作為篩選,放置不同sheet
2.並以篩選的class做sheet命名
VBA新手,在煩請指導
[attach]19678[/attach]
作者:
GBKEE
時間:
2014-12-1 10:45
本帖最後由 GBKEE 於 2014-12-1 10:51 編輯
回復
1#
jj369963
Option Explicit
Sub Ex_字典物件()
Dim d As Object, i As Integer, k As Variant, Sh As Worksheet
Set d = CreateObject("scripting.dictionary") '字典物件
i = 2
With Sheet1
Do While .Cells(i, "c") <> ""
d(.Cells(i, "c") & "") = "" '字典物件的 key : class欄的值
i = i + 1
Loop
For Each k In d '字典物件的 key
.Range("a1").AutoFilter 3, k '自動篩選 第3欄的準則為 k
On Error Resume Next
Set Sh = Sheets(k) 'k工作表不存在會有錯誤
If Err <> 0 Then
Set Sh = Sheets.Add(, Sheets(1))
Sh.Name = k
Err.Clear
On Error GoTo 0
End If
Sh.UsedRange.Clear
.UsedRange.Copy Sh.Range("A1")
Next
.Activate
.Range("a1").AutoFilter
End With
End Sub
Sub Ex_進階篩選()
Dim Rng As Range, Sh As Worksheet
With Sheet1
'進階篩選出class欄不重複的值
.Range("C:C").AdvancedFilter xlFilterCopy, , Cells(1, .Columns.Count), True
Set Rng = Cells(2, .Columns.Count)
Do While Rng <> ""
.Range("a1").AutoFilter 3, Rng '自動篩選 第3欄的準則為 Rng
On Error Resume Next
Set Sh = Sheets(Rng.Text) 'Rng.Text 工作表不存在會有錯誤
If Err <> 0 Then
Set Sh = Sheets.Add(, Sheets(1))
Sh.Name = Rng.Text
Err.Clear
On Error GoTo 0
End If
Sh.UsedRange.Clear
.UsedRange.Copy Sh.Range("A1")
Set Rng = Rng.Offset(1)
Loop
.Activate
.Range("a1").AutoFilter
Rng.EntireColumn.Clear
End With
End Sub
複製代碼
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)