標題:
如何利用勾選方塊來篩選資料
[打印本頁]
作者:
Jared
時間:
2015-6-8 16:44
標題:
如何利用勾選方塊來篩選資料
想請問各位大大
我現在想要利用表單來篩選資料
[attach]21133[/attach]
下拉式選擇資料還可以執行
但如果想要利用勾選資料的方式
不曉得問題點在哪裡 ><
[attach]21134[/attach]
Dim a, b, c, d, e, f, g, h, i, j, k, l, m
Private Sub CommandButton1_Click() '篩選條件
a = Test.ComboBox1.Value '季節
b = Test.ComboBox2.Value '月份
d = CheckBox1.Value
e = CheckBox2.Value
f = CheckBox3.Value
g = CheckBox4.Value
h = CheckBox5.Value
i = CheckBox6.Value
j = CheckBox7.Value
k = CheckBox8.Value
l = CheckBox9.Value
m = CheckBox10.Value
c = Array(d, e, f, g, h, i, j, k, l, m)
With ActiveSheet.Range("$A$1:$K$121")
.Parent.AutoFilterMode = False '顯示全部資料 ->新的 多重篩選
'多重篩選 ..........
If a <> "" Then .AutoFilter Field:=1, Criteria1:=a
If b <> "" Then .AutoFilter Field:=2, Criteria1:=b
If c <> "" Then .AutoFilter Field:=3, Criteria1:=c, Operator:=xlFilterValues
'多重篩選 ..........
End With
End Sub
Private Sub CommandButton2_Click() '清除內容
Me.ComboBox1 = ""
Me.ComboBox2 = ""
Me.CheckBox1 = ""
Me.CheckBox2 = ""
Me.CheckBox3 = ""
Me.CheckBox4 = ""
Me.CheckBox5 = ""
Me.CheckBox6 = ""
Me.CheckBox7 = ""
Me.CheckBox8 = ""
Me.CheckBox9 = ""
Me.CheckBox10 = ""
End Sub
Private Sub CommandButton3_Click() '取消
Test.Hide
End Sub
Private Sub CommandButton4_Click() '展開
ActiveSheet.Range("$A$1:$K$121").AutoFilter Field:=1
ActiveSheet.Range("$A$1:$K$121").AutoFilter Field:=2
ActiveSheet.Range("$A$1:$K$121").AutoFilter Field:=3
End Sub
Private Sub UserForm_Initialize()
ComboBox1.List = Array("Q1", "Q2", "Q3", "Q4") '季
ComboBox2.List = Array("一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月") '月
End Sub
複製代碼
作者:
luhpro
時間:
2015-6-11 23:35
本帖最後由 luhpro 於 2015-6-11 23:43 編輯
想請問各位大大
我現在想要利用表單來篩選資料
下拉式選擇資料還可以執行
但如果想要利用勾選資料的方 ...
Jared 發表於 2015-6-8 16:44
看起來自動篩選同一欄好像不能同時滿足超過一個條件. (即不能在同一欄中同時顯示出來小華與小明)
所以我這裡採用逐列篩選,
未能同時滿足所有條件的列則將其隱藏,
留下的就是我們要的資料了.
也因為採用的是隱藏列的方式,
所以 "按鈕1" 位置要上移到 第 1 列 以免被隱藏而無法點選.
首先, 因應程式調整, 全域變數的宣告內容也做調整:
Dim a, b, s, lRow
複製代碼
"篩選條件" 按鈕程式修改如下:
Private Sub CommandButton1_Click() '篩選條件
ActiveSheet.Rows.Hidden = False
a = Test.ComboBox1.Value '季節
b = Test.ComboBox2.Value '月份
s = ""
s = s & IIf(CheckBox1, CheckBox1.Caption & ",", "")
s = s & IIf(CheckBox2, CheckBox2.Caption & ",", "")
s = s & IIf(CheckBox3, CheckBox3.Caption & ",", "")
s = s & IIf(CheckBox4, CheckBox4.Caption & ",", "")
s = s & IIf(CheckBox5, CheckBox5.Caption & ",", "")
s = s & IIf(CheckBox6, CheckBox6.Caption & ",", "")
s = s & IIf(CheckBox7, CheckBox7.Caption & ",", "")
s = s & IIf(CheckBox8, CheckBox8.Caption & ",", "")
s = s & IIf(CheckBox9, CheckBox9.Caption & ",", "")
s = s & IIf(CheckBox10, CheckBox10.Caption & ",", "")
lRow = 2
With ActiveSheet
While .Cells(lRow, 1) <> ""
If Not (.Cells(lRow, 1) = a And .Cells(lRow, 2) = b And _
InStr(1, s, .Cells(lRow, 3)) <> 0) Then .Rows(lRow).Hidden = True
lRow = lRow + 1
Wend
End With
End Sub
複製代碼
"清除內容" 按鈕程式中,
CheckBox 的值在此不能設為 "" 而應設為 False 或 true ,
另外既然篩選的條件已經被清除了,
篩選結果自然應該一併做還原.
故修改如下:
Private Sub CommandButton2_Click() '清除內容
Me.ComboBox1 = ""
Me.ComboBox2 = ""
Me.CheckBox1 = False
Me.CheckBox2 = False
Me.CheckBox3 = False
Me.CheckBox4 = False
Me.CheckBox5 = False
Me.CheckBox6 = False
Me.CheckBox7 = False
Me.CheckBox8 = False
Me.CheckBox9 = False
Me.CheckBox10 = False
ActiveSheet.Rows.Hidden = False
End Sub
複製代碼
"展開" 按鈕程式也要跟著做調整:
Private Sub CommandButton4_Click() '展開
ActiveSheet.Rows.Hidden = False
End Sub
複製代碼
另外你的檔案 ComboBox2.List 內容並不能與 B 欄對應, (例如 : "一" 不能對應 "一月"),
同時可以考慮一併都賦與初值,
所以我做了以下的修改:
Private Sub UserForm_Initialize()
With ComboBox1
.List = Array("Q1", "Q2", "Q3", "Q4") '季
.Value = "Q1"
End With
With ComboBox2
.List = Array("一", "二", "三") '月
.Value = "一"
End With
End Sub
複製代碼
最後 Q1 不會有 "一","二","三" 以外的月份,
故可以在變更 ComboBox1 內容時一併把 ComboBox2 的選單調整好:
Private Sub ComboBox1_Change()
Select Case ComboBox1
Case "Q1"
ComboBox2.List = Array("一", "二", "三") '月
Case "Q2"
ComboBox2.List = Array("四", "五", "六") '月
Case "Q3"
ComboBox2.List = Array("七", "八", "九") '月
Case "Q4"
ComboBox2.List = Array("十", "十一", "十二") '月
End Select
End Sub
複製代碼
當然, 程式還有簡化的空間,
且 CheckBox 也可以考慮做物件陣列,
不過因為程式並不複雜,
所以這部份我就先不考慮了.
[attach]21161[/attach]
作者:
Jared
時間:
2015-6-26 09:30
回復
2#
luhpro
謝謝大大 :)
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)