创建自动过滤器代码
Creating an auto filter code
早上好,
我是 VBA 的新手,正在寻求帮助为我的 table 编写自动过滤代码。
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
任何建议将不胜感激
我会以不同的方式处理它:
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 的回答。
我建议修改、检查等:
您可能需要 Range = ActiveSheet.Cells(2, 2).Text
(或使用不同的名称,见下文)。这可能是错误的来源。另外,您的代码还有很多需要改进的地方。
使用Dim colstr as String
、colstr = ...
代替Range = ...
。
确保 TariffTable
定义正确。
AccountManagement
应该读作 AccountManage
.
确保 ActiveSheet
指的是您要使用的 Sheet
。
查询If colstr = "Enterprise" Then
而不是If colstr = True And colstr = "Enterprise" Then
(已使用更改后的名称)。
您可以改进使用多个 If
,例如 Select Case
,甚至可以将 colstr
与包含标题的 Range
匹配.
PS: 你没有 post 你的代码 output/errors。
早上好,
我是 VBA 的新手,正在寻求帮助为我的 table 编写自动过滤代码。
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
任何建议将不胜感激
我会以不同的方式处理它:
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 的回答。
我建议修改、检查等:
您可能需要
Range = ActiveSheet.Cells(2, 2).Text
(或使用不同的名称,见下文)。这可能是错误的来源。另外,您的代码还有很多需要改进的地方。使用
Dim colstr as String
、colstr = ...
代替Range = ...
。确保
TariffTable
定义正确。AccountManagement
应该读作AccountManage
.确保
ActiveSheet
指的是您要使用的Sheet
。查询
If colstr = "Enterprise" Then
而不是If colstr = True And colstr = "Enterprise" Then
(已使用更改后的名称)。您可以改进使用多个
If
,例如Select Case
,甚至可以将colstr
与包含标题的Range
匹配.
PS: 你没有 post 你的代码 output/errors。