使用VBA自动化Excel自动筛选器

时间:2017-12-19 15:03:39

标签: excel vba excel-vba autofilter

我目前正在使用以下VBA,但我需要稍微构建它。

Option Explicit
Sub AutoFilterData()
Dim wsData As Worksheet

Set wsData = ThisWorkbook.Worksheets("Master")
With wsData
    On Error Resume Next
    '//Reset Autofilter
    .ShowAllData
    ThisWorkbook.Worksheets("Master").Range("A1:BZ1").AutoFilter 2,
    ThisWorkbook.Worksheets("Summary-LT BD").Range("H1")
End With
 '//Close my objects
Set wsData = Nothing
End Sub

当我运行上面的代码时,即使填充单元格H1并且匹配主选项卡中第二列中的值,过滤器也会返回零结果。除了使用H1作为过滤器的一部分之外,我还需要能够使用单元格Q4-Q11从“Summary-LT BD”选项卡中添加额外的自动过滤标准。如果选择了值,则Q4-Q11 =“ “那么我需要显示该标准的所有值。我最终将这个宏添加到excel中的按钮单击作为一些表值的详细视图。

TIA提供任何帮助!

杰西

2 个答案:

答案 0 :(得分:1)

所以我有点把事情放在一起。我很亲密。所以前3个自动滤镜效果很好。我现在的问题是,当我在一个q范围内留下一个值为空时(当我想要没有对该值进行过滤时),我得到一个空白输出。我假设我需要某种if“”,在某种程度上显示“*”类型的语句。

    Option Explicit
    Sub NeedIn()
    Dim wsData As Worksheet

    Set wsData = ThisWorkbook.Worksheets("Master")

    With wsData
        On Error Resume Next
        '//Reset Autofilter
        .ShowAllData
        wsData.Range("A1:BZ1").AutoFilter 23, "Inside LT"
        wsData.Range("A1:BZ1").AutoFilter 75, "Need Date Moved In"
        wsData.Range("A1:BZ1").AutoFilter 2, ThisWorkbook.Worksheets("Summary-LT BD").Range("H1").Value
        wsData.Range("A1:BZ1").AutoFilter 4, ThisWorkbook.Worksheets("Summary-LT BD").Range("Q4").Value
        wsData.Range("A1:BZ1").AutoFilter 3, ThisWorkbook.Worksheets("Summary-LT BD").Range("Q5").Value
        wsData.Range("A1:BZ1").AutoFilter 5, ThisWorkbook.Worksheets("Summary-LT BD").Range("Q6").Value
        wsData.Range("A1:BZ1").AutoFilter 6, ThisWorkbook.Worksheets("Summary-LT BD").Range("Q7").Value
        wsData.Range("A1:BZ1").AutoFilter 7, ThisWorkbook.Worksheets("Summary-LT BD").Range("Q8").Value
        wsData.Range("A1:BZ1").AutoFilter 8, ThisWorkbook.Worksheets("Summary-LT BD").Range("Q9").Value
        wsData.Range("A1:BZ1").AutoFilter 9, ThisWorkbook.Worksheets("Summary-LT BD").Range("Q10").Value
        wsData.Range("A1:BZ1").AutoFilter 10, ThisWorkbook.Worksheets("Summary-LT BD").Range("Q11").Value

        Sheets("Master").Select
        Cells.Select
        Selection.Copy
        Workbooks.Add
        ActiveSheet.Paste
        Cells.Select
        Cells.EntireColumn.AutoFit

    End With
    Set wsData = ThisWorkbook.Worksheets("Master")

    With wsData
        On Error Resume Next
        '//Reset Autofilter
        .ShowAllData
     '//Close my objects
    End With
    Set wsData = Nothing
    End Sub

答案 1 :(得分:0)

以下内容如何:

Option Explicit
Sub AutoFilterData()
Dim wsData As Worksheet

Set wsData = ThisWorkbook.Worksheets("Master")

wsData.Range("$A$1:$BZ$1").AutoFilter Field:=2, Criteria1:=ThisWorkbook.Worksheets("Summary-LT BD").Range("H1")

'if you want to use multiple criteria to filter then you would have to use something like below (using an array of values to filter):
'wsData.Range("$A$1:$BZ$1").AutoFilter Field:=2, Criteria1:=Array("A", "B", "C", "D", "E"), Operator:=xlFilterValues
End Sub

<强>更新

这将允许您选择多个字段/列来过滤多个条件:

Option Explicit
Sub AutoFilterData()
Dim wsData As Worksheet
Dim LastRow As Long
Dim H1Value As String

Set wsData = ThisWorkbook.Worksheets("Master")
LastRow = wsData.Cells(wsData.Rows.Count, "A").End(xlUp).Row
'wsData.Range("$A$1:$BZ$" & LastRow).AutoFilter field:=2, Criteria1:=ThisWorkbook.Worksheets("Summary-LT BD").Range("H1")

'Or to filter multiple fields/columns then the code below will do it
H1Value = Worksheets("Summary-LT BD").Range("H1").Value
With wsData
.AutoFilterMode = False
.Range("A1:BZ" & LastRow).AutoFilter
.Range("A1:BZ" & LastRow).AutoFilter field:=2, Criteria1:=H1Value
.Range("A1:BZ" & LastRow).AutoFilter field:=23, Criteria1:="Inside LT"
.Range("A1:BZ" & LastRow).AutoFilter field:=75, Criteria1:="Other"
End With
End Sub