【问题标题】:Data Validation of a Filtered table过滤表的数据验证
【发布时间】:2015-11-29 10:36:41
【问题描述】:

我有一个带有自动过滤器的数据表(如下所示)。

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

由于我似乎无法弄清楚如何让我的组合框在过滤表格后仅显示可见单元格,我想知道是否有一种方法可以创建一个验证框来显示可见单元格或复制可见单元格数据到下面的单独表格中。然后我可以使用验证框/辅助表作为用户表单上组合框的焦点。

提前致谢

【问题讨论】:

    标签: vba validation excel filtering


    【解决方案1】:

    如果我正确理解了您的问题,您希望有一个数据验证下拉列表,该下拉列表会随着表格的过滤而更新,并且只显示给定列的可见项目。

    您可以通过在数据验证中使用以下公式来执行此操作(我假设您的表格标题行从 A1 开始,并且您需要显示它是 col A):

    =OFFSET($A$2,,,SUBTOTAL(103,TableName[列名]))

    此公式从起始单元格 (A2) 扩展指定的行数高度。我们使用函数编号 103 的 SUBTOTAL 定义高度 - 这意味着高度是使用 COUNTA 定义的,但仅在可见单元格上,因此它会在表格被过滤时展开和折叠。

    请注意:由于高度是使用 counta 函数定义的,因此它只会计算包含数据的单元格,因此如果表格中有空格,则范围将无法正确定义。此外,如果您有任何重复数据,这些数据将在您的下拉框中重复,此方法不会将它们压缩成一个整洁、唯一的列表。

    希望这会有所帮助。 D

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多