创建自动过滤器代码

时间:2015-05-28 09:53:36

标签: excel vba excel-vba autofilter

早上好,

我是VBA的新手,并且正在为我的桌子编写自动过滤器代码寻求帮助。

Tariffs     |   SME100  |   Enterprise  |   CustomerLoyalty |   AccountManage   
------------+-----------+---------------+-------------------+-------------------      
Voda Red    |   1       |     1         |         0         |          1
Voda 1G D   |   1       |     0         |         1         |          0

1* eligible to sell
0* not eligible sell

我正在尝试编写一个代码,该代码从验证框中获取值(" B2")并自动过滤该销售渠道的特定列以获得符合条件的关税。我目前的代码是:

Sub Filter()
    Dim strRange As String
    strRange = "B"
    Dim b As Integer
    b = "2"
    Range = ActiveSheet.Cells(2, 2).Address(False, False)

    If Range = True And Range = "SME100" Then
        ActiveSheet.ListObjects("TariffTable").Range.AutoFilter Field:=2, Criteria1:="1"

    If Range = True And Range = "Enterprise" Then
        ActiveSheet.ListObjects("TariffTable").Range.AutoFilter Field:=3, Criteria1:="1"

    If Range = True And Range = "CustomerLoyalty" Then
        ActiveSheet.ListObjects("TariffTable").Range.AutoFilter Field:=4, Criteria1:="1"

    If Range = True And Range = "AccountManagement" Then
        ActiveSheet.ListObjects("TariffTable").Range.AutoFilter Field:=5, Criteria1:="1"
    Else
        MsgBox ("No Sales Channel Selected")
    End If   
End Sub

任何建议都将非常感谢

2 个答案:

答案 0 :(得分:1)

我会以不同的方式接近它:

Sub Filter()

    Dim columnNumber, tableRow, tableColumn, tableWidth As Integer
    Dim tableName, columnName As String

    tableName = "Table1"
    columnName = ActiveSheet.range("B2").Value

    'This clears the existing filter
    ActiveSheet.ListObjects(tableName).range.AutoFilter

    'Assign some numbers we need to know about the table to check the headers
    tableRow = ActiveSheet.ListObjects(tableName).range.Row
    tableColumn = ActiveSheet.ListObjects(tableName).range.Column
    tableWidth = ActiveSheet.ListObjects(tableName).range.Columns.Count

    'If a column title with the specified value does not exist VBA throws an error which we need to catch, this is one of the many reasons I dislike VBA :D
    On Error GoTo ErrorHandler
    'Search through the table column header row to find the specified column and assign the number to columnNumber
    columnNumber = Application.WorksheetFunction.Match(columnName, range(Cells(tableRow, tableColumn), Cells(tableRow, tableColumn + tableWidth)), 0)

    'Apply the filter "1" to the found columnNumber
    ActiveSheet.ListObjects(tableName).range.AutoFilter Field:=columnNumber, Criteria1:="1"

    'Exit the sub otherwise the "error handling" will be provoked
    Exit Sub

ErrorHandler:

    MsgBox columnName & " does not exist"

End Sub

编辑:另外,你应该阅读并理解sancho.s的答案。

答案 1 :(得分:0)

我建议修改,检查等:

  1. 您可能需要Range = ActiveSheet.Cells(2, 2).Text(或使用其他名称,请参阅下文)。这可能是错误的根源。此外,您的代码还有很多需要改进的地方。

  2. 使用Dim colstr as Stringcolstr = ...代替Range = ...

  3. 确保TariffTable已正确定义。

  4. AccountManagement应阅读AccountManage

  5. 确保ActiveSheet引用您要使用的Sheet

  6. 查询If colstr = "Enterprise" Then而不是If colstr = True And colstr = "Enterprise" Then(已使用更改的名称)。

  7. 您可以改进使用多个If,例如Select Case,甚至匹配colstr与包含标题的Range

  8. PS:您没有发布代码的输出/错误。