返回列表 上一主題 發帖

VBA做篩選

回復  sunnyso
    說錯...
是做排序篩選..因筆數有上千筆...要從表單中抓出重覆
sillykin 發表於 2013/8/31 13:34

[排序篩選..  抓出重覆 ]  請定義: 哪裡的重覆
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 6# sillykin
試試看
  1. Option Explicit
  2. Sub Ex()
  3.     Dim Rng(1 To 3) As Range, i As Integer, E As Range
  4.     With Sheets("Sheet1")          ' "Sheet1" 工作表名稱
  5.         .Cells.Interior.ColorIndex = xlNone
  6.         Set Rng(1) = .Range("A:F").SpecialCells(xlCellTypeConstants)                              '資料庫
  7.         .Range("G:G") = ""
  8.         Set Rng(3) = Rng(1).Rows(1)
  9.         For i = 3 To 5                                      'C欄、D欄、E欄位做為準則
  10.             .Cells(1, .Columns.Count) = Rng(1).Cells(1, i)  '欄位做為準則
  11.             Rng(1).Columns(i).AdvancedFilter xlFilterCopy, , .Cells(1, .Columns.Count), True       '篩選不重複的資料
  12.             Set Rng(2) = .Range(.Cells(2, .Columns.Count), .Cells(2, .Columns.Count).End(xlDown))  '篩選出的資料範圍
  13.             For Each E In Rng(2)
  14.                 If Application.CountIf(Rng(1).Columns(i), E) > 1 Then                              ' 資料在資料庫裡的資料數大於1
  15.                     With Rng(1).Columns(i).Cells
  16.                         .Replace E, "=XXX", xlWhole                                                '更改為錯誤值
  17.                         With .SpecialCells(xlCellTypeFormulas, xlErrors)                           '錯誤值的特殊範圍裡
  18.                             .Value = E                                                             '置回原來的資料
  19.                             Set Rng(3) = Union(Rng(3), .Cells)                                     '加入範圍
  20.                             .Interior.Color = vbYellow
  21.                             .Offset(, Rng(1).Columns.Count + 1 - i) = "重覆請查核"
  22.                         End With
  23.                     End With
  24.                 End If
  25.             Next
  26.         Next
  27.         .Cells(1, .Columns.Count).EntireColumn = ""
  28.         Set Rng(3) = Application.Intersect(.Cells, Rng(3).EntireRow)  '整合為整列
  29.     End With
  30.     With Sheets("Sheet2")
  31.         .Cells.Clear
  32.         Rng(3).Copy .Range("A1")
  33.         .Cells.Interior.ColorIndex = xlNone
  34.         .Cells.EntireColumn.AutoFit
  35.     End With
  36. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 8# c_c_lai
.Replace E, "=XXX", xlWhole  的作用何在?  將資料庫要搜尋的字串一次變為無效的公式(錯誤值)
With .SpecialCells(xlCellTypeFormulas, xlErrors) ->範圍中的特殊儲存(錯誤值)
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 11# sillykin
Rng(3)在程式中執行一直是不連續的區塊(欄數位置不一樣),無法用Copy 的方法
Set Rng(3) = Application.Intersect(.Cells, Rng(3).EntireRow)  '整合為整列(欄數位置一樣)可一起Copy 複製到其他地方
  1. EntireRow 屬性
  2. 請參閱套用至範例特定傳回 Range 物件,該物件代表包含指定範圍的整個列 (或若干列)。唯讀
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 15# sillykin
  1. Option Explicit
  2. Private Sub UserForm_Initialize()
  3.     Dim D(1 To 6) As Object, i As Integer, R As Variant
  4.     For i = 1 To 6
  5.         Set D(i) = CreateObject("Scripting.Dictionary")
  6.         With Sheet1
  7.             For Each R In .Range("A2", .[A2].End(xlDown)) '
  8.                  D(i)(R.Offset(, i - 1).Value) = ""
  9.             Next
  10.         End With
  11.         Controls("ComboBox" & i).List = Application.Transpose(D(i).keys)
  12.         'ComboBox六個選項須重新,依序命名 ComboBox1(組別) ...-> ComboBox6(元)
  13.     Next
  14. End Sub
  15. Private Sub CommandButton1_Click() '篩選條件
  16.     Dim Rng As Range, i As Integer
  17.     Application.ScreenUpdating = False
  18.     Set Rng = ActiveSheet.Range("$A$1:$Q$300")
  19.     Rng.Parent.AutoFilterMode = False   '顯示全部資料 ->新的 多重篩選 才會確.
  20.     '多重篩選  ..........
  21.     For i = 1 To 6
  22.         With Controls("ComboBox" & i)
  23.             If .Value <> "" Then
  24.                 Rng.AutoFilter Field:=i, Criteria1:=.Value & "*" '
  25.             End If
  26.         End With
  27.     Next
  28.     Application.ScreenUpdating = True
  29. End Sub
  30. Private Sub CommandButton4_Click()
  31.     With ActiveSheet.Range("$A$1:$Q$300") '範圍
  32.         .Parent.AutoFilterMode = False   '顯示全部資料 ->取消 多重篩選
  33.     End With
  34. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

本帖最後由 GBKEE 於 2013-9-4 10:36 編輯

回復 18# sillykin
  1. Option Explicit
  2. Option Base 1  '<- 下限值為 1  ; 若要設下限值為 0,則 Option Base 陳述式是不需要的。

  3. Dim Ar(), Ax()                            '這模組中的程式可用之變數
  4. Private Sub UserForm_Initialize()
  5.     Dim D(1 To 6) As Object, i As Integer, R As Variant
  6.     '********如果SHEET1欄位值為A~D欄位及F欄位及G欄位..***
  7.     Ar = Array(1, 2, 3, 4, 6, 7)   '設定欄位
  8.     '****************************************************
  9.     Ax = Array(ComboBox1, ComboBox2, ComboBox3, ComboBox4, ComboBox5, ComboBox6)    'ComboBox六個選項內容依序為 組別,姓名1....
  10.     With Sheet1
  11.         .AutoFilterMode = False   '顯示全部資料 ->新的 多重篩選 才會確.
  12.         For i = 1 To 6
  13.             Set D(i) = CreateObject("Scripting.Dictionary")
  14.             For Each R In .Range("A2", .[A2].End(xlDown))
  15.                  '************ 若預設的下限值為 0  ->   i - 1   *******************************************************
  16.                  'D(i)(R.Offset(, Ar(i - 1) - 1).Value) = ""    'i = 1 時 若預設的下限值為0 則需Ar(i - 1)-> Ar(0) = 1'*
  17.                  '*****************************************************************************************************
  18.                  D(i)(R.Offset(, Ar(i) - 1).Value) = ""
  19.             Next
  20.             Ax(i).List = Application.Transpose(D(i).keys)
  21.         Next
  22.     End With
  23. End Sub
  24. Private Sub CommandButton1_Click() '篩選條件
  25.     Dim Rng As Range, i As Integer
  26.     Application.ScreenUpdating = False
  27.     Set Rng = ActiveSheet.Range("$A$1:$Q$300")
  28.     Rng.Parent.AutoFilterMode = False       '顯示全部資料 ->新的 多重篩選 才會確.
  29.     For i = 1 To 6                          '多重篩選  ..........
  30.         '************ 若預設的下限值為 0  ->   i - 1   *****************************************************
  31.         'If Ax(i - 1).Value <> "" Then Rng.AutoFilter Field:=Ar(i - 1), Criteria1:=Ax(i - 1).Value & "*"  '*
  32.         '***************************************************************************************************
  33.         If Ax(i).Value <> "" Then Rng.AutoFilter Field:=Ar(i), Criteria1:=IIf(i <> 5, Ax(i).Value & "*", Ax(i).Value)
  34.                                                                '元(F欗)為數值不可用 * 來篩選
  35.     Next
  36.     Application.ScreenUpdating = True
  37. End Sub
  38. Private Sub CommandButton4_Click()
  39.     With ActiveSheet.Range("$A$1:$Q$300") '範圍
  40.         .Parent.AutoFilterMode = False   '顯示全部資料 ->取消 多重篩選
  41.     End With
  42. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 20# sillykin
可依樣畫葫蘆試試看,有問題可再提問(多練習VBA會進步的)
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 18# sillykin
  1. Option Explicit
  2. Sub Ex()
  3.     Dim Rng(1 To 3) As Range, i As Integer, E As Range
  4.     'With Sheets("Sheet1")          ' "Sheet1" 工作表名稱
  5.    
  6.     With Sheet1                     ' Sheet1  工作表物件名稱
  7.         .Cells.Interior.ColorIndex = xlNone
  8.         Set Rng(1) = .Range("A:F").SpecialCells(xlCellTypeConstants)                                    '資料庫
  9.         .Range("G:G") = ""
  10.         Set Rng(3) = Rng(1).Rows(1)
  11.         For i = 1 To 7                                                                                  'C欄、D欄、E欄位做為準則
  12.            MsgBox .OLEObjects("CheckBox" & i).Object
  13.             If .OLEObjects("CheckBox" & i).Object.Value = True Then     '有勾選=.Value = True       *****
  14.                 .Cells(1, .Columns.Count) = Rng(1).Cells(1, i)                                          '欄位做為準則
  15.                 Rng(1).Columns(i).AdvancedFilter xlFilterCopy, , .Cells(1, .Columns.Count), True        '篩選不重複的資料
  16.                 Set Rng(2) = .Range(.Cells(2, .Columns.Count), .Cells(2, .Columns.Count).End(xlDown))   '篩選出的資料範圍
  17.                 For Each E In Rng(2)
  18.                     If Application.CountIf(Rng(1).Columns(i), E) > 1 Then                               ' 資料在資料庫裡的資料數大於1
  19.                         With Rng(1).Columns(i).Cells
  20.                             .Replace E, "=XXX", xlWhole                                                 '更改為錯誤值
  21.                             With .SpecialCells(xlCellTypeFormulas, xlErrors)                            '錯誤值的特殊範圍裡
  22.                                 .Value = E                                                              '置回原來的資料
  23.                                 Set Rng(3) = Union(Rng(3), .Cells)                                      '加入範圍
  24.                                 .Interior.Color = vbYellow
  25.                                 .Offset(, Rng(1).Columns.Count + 1 - i) = "重覆請查核"
  26.                             End With
  27.                         End With
  28.                     End If
  29.                 Next
  30.             End If
  31.         Next
  32.         .Cells(1, .Columns.Count).EntireColumn = ""
  33.         Set Rng(3) = Application.Intersect(.Cells, Rng(3).EntireRow)  '整合為整列
  34.     End With
  35.     With Sheets("Sheet2")
  36.         .Cells.Clear
  37.         Rng(3).Copy .Range("A1")
  38.         .Cells.Interior.ColorIndex = xlNone
  39.         .Cells.EntireColumn.AutoFit
  40.     End With
  41. End Sub
複製代碼


感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

        靜思自在 : 君子為目標,小人為目的。
返回列表 上一主題