过滤后的数据验证 table
Data Validation of a Filtered table
我有一个带自动过滤器的数据 table(如下所示)。
Sub Tariff_Filter()
Dim columnNumber, tableRow, tableColumn, tableWidth As Integer
Dim tableName, columnName As String
tableName = "Tariff_Table"
columnName = ActiveSheet.Range("A1").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
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 & "Please Specify Required Channel"
End Sub
因为我似乎无法弄清楚如何让我的组合框在过滤后仅显示可见单元格 table 我想知道是否有办法创建一个验证框来显示可见单元格或将可见数据复制到下面的单独 table 中。然后我可以使用验证框/辅助 table 作为用户表单上组合框的焦点。
提前致谢
如果我对你的问题的理解正确,你希望有一个 data-validation drop-down 列表随着 table 被过滤而更新并且只显示给定的可见项目列。
您可以通过在数据验证中使用以下公式来执行此操作(我假设您的 table header 行从 A1 开始并且您需要显示的是 A 列):
=OFFSET($A$2,SUBTOTAL(103,TableName[列名]))
此公式从起始单元格 (A2) 扩展指定的行数高度。我们使用功能编号为 103 的 SUBTOTAL 定义高度 - 这意味着高度是使用 COUNTA 定义的,但仅限于可见单元格,因此它会随着 table 被过滤而展开和折叠。
请注意:由于高度是使用 counta 函数定义的,它只会对包含数据的单元格进行计数,因此如果您的 table 中有空格,则不会正确定义范围。此外,如果您有任何重复的数据,这些将在您的 drop-down 框中重复,此方法不会将它们压缩成一个整齐、独特的列表。
希望这对您有所帮助。
D
我有一个带自动过滤器的数据 table(如下所示)。
Sub Tariff_Filter()
Dim columnNumber, tableRow, tableColumn, tableWidth As Integer
Dim tableName, columnName As String
tableName = "Tariff_Table"
columnName = ActiveSheet.Range("A1").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
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 & "Please Specify Required Channel"
End Sub
因为我似乎无法弄清楚如何让我的组合框在过滤后仅显示可见单元格 table 我想知道是否有办法创建一个验证框来显示可见单元格或将可见数据复制到下面的单独 table 中。然后我可以使用验证框/辅助 table 作为用户表单上组合框的焦点。
提前致谢
如果我对你的问题的理解正确,你希望有一个 data-validation drop-down 列表随着 table 被过滤而更新并且只显示给定的可见项目列。
您可以通过在数据验证中使用以下公式来执行此操作(我假设您的 table header 行从 A1 开始并且您需要显示的是 A 列):
=OFFSET($A$2,SUBTOTAL(103,TableName[列名]))
此公式从起始单元格 (A2) 扩展指定的行数高度。我们使用功能编号为 103 的 SUBTOTAL 定义高度 - 这意味着高度是使用 COUNTA 定义的,但仅限于可见单元格,因此它会随着 table 被过滤而展开和折叠。
请注意:由于高度是使用 counta 函数定义的,它只会对包含数据的单元格进行计数,因此如果您的 table 中有空格,则不会正确定义范围。此外,如果您有任何重复的数据,这些将在您的 drop-down 框中重复,此方法不会将它们压缩成一个整齐、独特的列表。
希望这对您有所帮助。 D