2017-07-04 67 views
1

我設計了一個帶有7個複選框的用戶表單。每個複選框都有一個調用函數,稱爲自動過濾器。這個功能自動過濾器,有助於過濾我的工作表中的字段4並顯示結果。VBA使用複選框選擇多個案例

在這裏,我有3個案例。案例1:

案例1。當選中Checkbox1時,顯示autofilter1的結果。

情形2:被選擇checkboxes1和2時,顯示autofilter1的結果和2

注意:它是沒有必要的用戶可以只選擇檢查框1和2,也可以是複選框2和3或1還有3次或者全部3次選擇。

案例3:當沒有選擇任何東西時,清除過濾器。

我成功地生成了case1情況,我應該如何繼續,才能實現case2和case3。

下面是自動篩選功能,它被分配到checkbox1。

Sub autofilter1() 
Dim ws As Worksheet 
Set ws = ThisWorkbook.Sheets("Result") 
wslr = ws.Cells(Rows.Count, 1).End(xlUp).Row 
Set myfilt = ws.Range("A1:D" & wslr) 
myfilt.autofilter Field:=4, Criteria1:= _ 
"USA" 
End Sub 

autofilter函數分配給checkbox2。分配給複選框3.

Sub autofilter3() 
Dim ws As Worksheet 
Set ws = ThisWorkbook.Sheets("Result_APQP") 
wslr = ws.Cells(Rows.Count, 1).End(xlUp).Row 
Set myfilt = ws.Range("A1:D" & wslr) 
myfilt.autofilter Field:=4, Criteria1:= _ 
"France" 
End Sub 

在命令按鈕 「轉到」

Sub autofilter2() 
Dim ws As Worksheet 
Set ws = ThisWorkbook.Sheets("Result") 
wslr = ws.Cells(Rows.Count, 1).End(xlUp).Row 
Set myfilt = ws.Range("A1:D" & wslr) 
myfilt.autofilter Field:=4, Criteria1:= _ 
"Germany" 
End Sub 

自動篩選功能,我有以下的代碼,

Private Sub CommandButton4_Click() 
If CheckBox1.Value = True Then 
Call autofilter1 
End If 
If CheckBox2.Value = True Then 
Call autofilter2 
End If 
If CheckBox3.Value = True Then 
Call autofiletr3 
End If 
End Sub 

我重視的形象也,以供參考。在圖片中,我剛剛以3複選框爲例。 This is the example of case1, where one Checkbox is selected and the displays the result of autofilter1. This is the example of case2, where the checkbox1 and 2 are selected and Displays the result of autofilter1 and 2. Case3, is where nothing is selected and it Displays the original sheet, Clearing the filter.

+0

下一次sumarize有點plz,只是問基礎問題。爲每個案例分配一個子集,並'選擇案例true:案例checkbox1.value:sub1:案例check2:sub2 ..... end select'。對不起,我在電話應用程序,無法更好地格式化代碼。 –

+0

雅,下次我會這樣做 – Mikz

回答

1

。這是一個用戶窗體模塊代碼。

我用3個複選框做了一個UserForm。

每個事件處理程序只將觸發DoFilter()方法,該方法檢查複選框和用於是真實的那些,構建字符串數組基於它們Caption(如在您的示例圖像)並傳遞到陣列自動篩選。如果數組爲空,則自動過濾器被禁用。

編輯: 在Visual Basic編輯器(ALT + F11),在項目窗口中右鍵單擊。在菜單中選擇Insert -> UserForm。在此UserForm上,添加一些CheckBoxes。對於每個複選框,請將屬性窗口中的Caption屬性更改爲您想要在過濾的範圍內顯示的值,例如根據該問題,CheckBox1.Caption將是「美國」。接下來,右鍵單擊用戶表單模塊並選擇View Code。在代碼窗口中,粘貼下面的所有內容:

Private Sub DoFilter() 

Dim ws As Worksheet 
Dim strCriteria() As String 
Dim arrIdx As Integer 

Dim cBox As Control 

arrIdx = 0 
For Each cBox In Me.Controls 
    If TypeName(cBox) = "CheckBox" Then 
     If cBox.Value = True Then 
      ReDim Preserve strCriteria(0 To arrIdx) 
      strCriteria(arrIdx) = cBox.Caption 
      arrIdx = arrIdx + 1 
     End If 
    End If 
Next cBox 

Set ws = ThisWorkbook.Sheets("Sheet2") 
If arrIdx = 0 Then 
    ws.UsedRange.AutoFilter 
Else 
    ws.Range("A:D").AutoFilter Field:=4, Criteria1:=Array(strCriteria), Operator:=xlFilterValues 
End If 

End Sub 

Private Sub CheckBox1_Change() 'Repeat for each CheckBox on your form. 
    DoFilter 
End Sub 
Private Sub CheckBox2_Change() 'Repeat for each CheckBox on your form. 
    DoFilter 
End Sub 
Private Sub CheckBox3_Change() 'Repeat for each CheckBox on your form. 
    DoFilter 
End Sub 

**其他編輯:** N.b。在這種情況下,只要更改複選框,過濾器就會觸發。如果你想申請一個命令按鈕的點擊過濾器,你也可以把下面的代碼的用戶窗體模塊中,而不是Private Sub CheckBox#_Change()

Private Sub CommandButton4_Click() 
    DoFilter 
End Sub 

你可能想爲了不使每個使用Control Array單獨列出的CheckBox_Change事件處理程序。

+0

你能告訴我在哪裏添加這個Private Sub do filter(),它是模塊嗎? – Mikz

+0

在這種情況下,在用戶窗體中。 –

+0

意思是,userform點擊() – Mikz