返回列表 上一主題 發帖

可以利用VBA進行多條件篩選嗎?

回復 3# GBKEE


    發現這是幾年前的帖子了。換問問版主。
a = Test5.TextBox1.Text
b = Test5.ComboBox1.Value
c = Test5.ComboBox2.Value
d = Test5.ComboBox3.Value。大大請問test5。這個地方是怎麼設計的。小弟權限不足不能下載來看。抱歉

TOP

回復 4# Jared


   a = Test5.TextBox1.Text
b = Test5.ComboBox1.Value
c = Test5.ComboBox2.Value
d = Test5.ComboBox3.Value。大大請問test5。這個地方是怎麼設計的。小弟權限不足不能下載來看。抱歉

TOP

謝謝分享。

TOP

回復 5# jeffrey628litw


你可以把樓頂問題的測試檔下載下來
在把程式碼替換掉就可以了
介面上應該是沒什麼改到
只有在程式碼的部分
Jared

TOP

2位大大,小弟剛在學習VBA,可以將您們討論的結果,檔案上傳供我們下載嗎?感恩

TOP

回復 3# GBKEE


感謝大大解除了我的疑惑
原本的程式碼是寫成獨立式
  1. If a <> "" Then
  2. ActiveSheet.Range("$A$1:$E$12").AutoFilter Field:=1, Criteria1:=a: Exit Sub
  3. End If
複製代碼
後來有修改成
  1. Private Sub CommandButton4_Click()
  2. Dim a, b, c, d, e
  3. a = Test5.TextBox1.Text
  4. b = Test5.ComboBox1.Value
  5. c = Test5.ComboBox2.Value
  6. d = Test5.ComboBox3.Value
  7. e = Test5.TextBox2.Text

  8. If a <> "" Or b <> "" Or c <> "" Or d <> "" Or e <> "" Then
  9. With Sheet2.Range("$A$1:$E$12")
  10. .AutoFilter Field:=1, Criteria1:=a
  11. .AutoFilter Field:=2, Criteria1:=b
  12. .AutoFilter Field:=3, Criteria1:=c
  13. .AutoFilter Field:=4, Criteria1:=d
  14. .AutoFilter Field:=5, Criteria1:=e

  15. End With
  16. End If
  17. End Sub
複製代碼
但卻變成要每個條件都符合才會篩選出東西>"<
看了大大的解答
原來是要分開進行

看來我又學到一課了^口^
非常感謝!!
Jared

TOP

回復 2# Jared
  1. Option Explicit
  2. Private Sub CommandButton4_Click() '篩選條件
  3. Dim a, b, c, d, e
  4. a = Test5.TextBox1.Text
  5. b = Test5.ComboBox1.Value
  6. c = Test5.ComboBox2.Value
  7. d = Test5.ComboBox3.Value
  8. e = Test5.TextBox2.Text
  9. With ActiveSheet.Range("$A$1:$E$12")
  10.     .Parent.AutoFilterMode = False   '顯示全部資料 ->新的 多重篩選 才會確.
  11.     '多重篩選  ..........
  12.     If a <> "" Then .AutoFilter Field:=1, Criteria1:=a
  13.     If b <> "" Then .AutoFilter Field:=2, Criteria1:=b
  14.     If c <> "" Then .AutoFilter Field:=3, Criteria1:=c
  15.     If d <> "" Then .AutoFilter Field:=4, Criteria1:=d
  16.     If e <> "" Then .AutoFilter Field:=5, Criteria1:=e
  17.     '多重篩選  ..........
  18. End With
  19. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 1# Jared


發現利用陸製巨集的方式
可以一次篩選一個條件
那如果是要兩個以上
要怎麼設定呢?
  1. Private Sub CommandButton4_Click() '篩選條件
  2. Dim a, b, c, d, e
  3. a = Test5.TextBox1.Text
  4. b = Test5.ComboBox1.Value
  5. c = Test5.ComboBox2.Value
  6. d = Test5.ComboBox3.Value
  7. e = Test5.TextBox2.Text
  8. If a <> "" Then
  9. ActiveSheet.Range("$A$1:$E$12").AutoFilter Field:=1, Criteria1:=a: Exit Sub
  10. End If

  11. If b <> "" Then
  12. ActiveSheet.Range("$A$1:$E$12").AutoFilter Field:=2, Criteria1:=b: Exit Sub
  13. End If

  14. If c <> "" Then
  15. ActiveSheet.Range("$A$1:$E$12").AutoFilter Field:=3, Criteria1:=c: Exit Sub
  16. End If

  17. If d <> "" Then
  18. ActiveSheet.Range("$A$1:$E$12").AutoFilter Field:=4, Criteria1:=d: Exit Sub
  19. End If

  20. If e <> "" Then
  21. ActiveSheet.Range("$A$1:$E$12").AutoFilter Field:=5, Criteria1:=e: Exit Sub
  22. End If
  23. End Sub
複製代碼
Jared

TOP

        靜思自在 : 君子如水,隨方就圓,無處不自在。
返回列表 上一主題