利用工作表中的控件(组合框)来控制自动筛选

时间:2019-05-16 13:06:28

标签: excel vba

将过滤后的数据复制到另一个工作表中,用户可以在其中通过ComboBox(用户窗体或ActiveX)输入过滤条件

最终,我想将选择数据从一个工作表上的主数据集中复制到另一工作表上。为了隔离相关数据以进行跨复制,我计划使用过滤器。我想在工作表中包含一个下拉列表对象,该列表对象将允许用户选择过滤条件

这实际上只是复制Excel数据过滤器功能,而不受位置限制在要过滤的列上。

我假设Userform或ActiveX ComboBox是实现此目的的方法?这是我失败的地方。我不知道如何调用用户从ComboBox中选择的值来通知自动筛选过程。

Sub M_00()

    Dim wB As Workbook
    Dim wS1, wS2 As Worksheet
    'Dim x As ???
    Dim rng3 As Range
    Dim lrowS3 As Long

    Set wB = This.Workbook
    Set wS1 = wB.Sheets(1)
    Set wS3 = wB.Sheets(3)

    'define range for Combo Box drop down list from data on Sheet 3
    lrowS3 = wS3.Cells(Rows.Count, 1).End(xlUp).Row

    Set rng3 = wS3.Range(Cells(2, 2), Cells(lrowS3, 2))

    'Don't know how to
    ' a) assign my rng3 values to the ComboBox drop down list
    ' b) call the selection from the ComboBox to inform my AutoFilter

    wS1.ComboBox1.List = rng3.Value

    x = wS1.ComboBox1.Selection

    ' turn off any autofilters that may already be active in Sheet 1
    wS1.AutoFilterMode = False

    ' filter column 4 based on combo box selection
    wS1.UsedRange.AutoFilter Field:=4, Criteria1:=x

    'Once filtered I plan you assign the visible data to an array and copy 
    'it to the required sheet.
End Sub

3 个答案:

答案 0 :(得分:0)

基本上,自动筛选器的作用是隐藏与选择权不匹配的整个行。在此示例中,我假设您要在B列中查找的信息

'getting the value I am looking for
  cbVal = comboBox1.Text

 'getting the last row filled
 lr = activesheet.cells(rows.count,2).end(xlup).row


'asuming that B1 is the title of the table, so I'm starting since B2 to last row

for i = 2 to lr
if not activesheet.range("B"&i) == cbVal then
  activesheet.range("B"&i).entireRow.Hidden = true
end if
next i

最后,您将anothe sub设置为取消隐藏行

for i = 2 to lr
activesheet.range("B"&i).hidden = false
next i

希望它能解决您的问题,我还没有测试过,但您的想法是您了解逻辑

答案 1 :(得分:0)

所以我不确定我能100%得到您的尝试,但这是一种方法:

以下方法适用于ActiveX组合框。

为简单起见,我们假设您具有以下设置:

enter image description here

过滤器值在A1:A8中,并且您要按天的名称过滤C列。

首先,您需要一个用于初始化组合框的子项。这可以位于您的模块中。 sub的调用方式取决于您。如果列表需要定期更新,您可以为此设置一个专用按钮。

Sub initializeComboBox() 'The name of the Sub is self explanatory

Dim sht As Worksheet
Set sht = ThisWorkbook.Worksheets("Name of your Worksheet") 'The worksheet where the combobox is located
With sht.OLEObjects("ComboBox1").Object 'Referring to the combobox from outside the worksheet where it's located can be a bit tricky
    .Clear 'Clear the combobox's list
    .List = sht.Range("A1:A8").Value 'assign the values contained in range A1:A8 to the combobox's list
End With

End Sub

然后,您需要一个事件,该事件将在用户选择一个时捕获组合框值的变化。这需要位于组合框所属的工作表中。

Private Sub ComboBox1_Change() 'This event fires up whenever the user selects an item from the dropdown menu
Dim x As String
x = ComboBox1.Value 'referring to the combobox from inside the worksheet where it belongs is easier
Module1.testFilter (x)
End Sub

然后,您可以在模块中拥有一个sub并被该事件调用,或者可以使用事件本身来执行您想做的任何事情。我在这里应用了第一种方法,但这实际上是您的选择。

Sub testFilter(filterValue As String) 'A sub that is located in the module and applies the filter. This is called whenever the Value of the combobox is changed

Dim sht As Worksheet
Dim rngToBeFiltered As Range
Set sht = ThisWorkbook.Worksheets("Name of your Worksheet") 'The worksheet where the range that needs to be filtered is located
Set rngToBeFiltered = sht.Columns("C")
If filterValue = "No Filter" Then
    rngToBeFiltered.AutoFilter field:=1 'If No Filter is selected then all data is displayed
Else
    rngToBeFiltered.AutoFilter field:=1, Criteria1:=filterValue 'else the filter is applied
End If

End Sub

结果如下:

enter image description here

答案 2 :(得分:0)

我最终将操作分为两个模块M_00_Filter()和M_00_Generate()。

第一个过滤器根据用户表单组合框中的选择过滤数据,该组合框静态分配了同一工作簿中另一个工作表的范围。

Sub M_00_Filter()

Dim wB As Workbook
Dim myindex As Long
Dim filterValue As String
Dim wS As Worksheet
Dim rng2BF As Range

Set wB = ThisWorkbook
Set wS = wB.Sheets("Master Schedule")

'~~> Currently selected item index at runtime
myindex = wS.Shapes("Drop Down 1").ControlFormat.Value

'~~> Currently selected item value at runtime
filterValue = wS.Shapes("Drop Down 1").ControlFormat.List(myindex)

lRow = wS.Cells(wS.Rows.Count, 4).End(xlUp).Row
lCol = wS.Cells(5, wS.Columns.Count).End(xlToLeft).Column

'Set rngToBeFiltered
Set rng2BF = wS.Range(Cells(5, 1), Cells(lRow, lCol))

If filterValue = "Select a Phase" Then

    rng2BF.AutoFilter field:=4 'If No Filter is selected then all data is displayed

Else

    rng2BF.AutoFilter field:=4, Criteria1:=filterValue 'else the filter is applied

End If

End Sub

第二个模块复制可见数据并将其复制到另一个工作簿中。 再次感谢您的帮助。

感谢您抽出宝贵的时间。 干杯。