【问题标题】:VBA cannot AutoFilter a Range for a certain criteriaVBA 无法根据特定条件自动筛选范围
【发布时间】:2023-02-02 05:46:43
【问题描述】:

我正在尝试为所有条目创建一个 Range 对象,而不是应用一个过滤器,该过滤器在其中搜索一个数字。 我希望 Range 之后只保留匹配的条目,但我总是收到错误 1004...

这里的代码:

Dim rSearch As Range
Dim rResult As Range

Set rSearch = wbMe.Sheets(iCurSheet).Range("F2:F1000")
rSearch.AutoFilter Field:=iColKey, Criteria1:="=" & wbMe.Sheets(iCurSheet).Cells(iLine, iColKey).Value

最后一行抛出异常。我发现 AutoFilter 必须应用于第一行,所以 .Range("A1:K1"),但我仍然不明白为什么我不能在范围上进行过滤,也许我弄错了对象?

提前致谢!

编辑: 所以我尝试了一些东西:

Set rSearch = wbMe.Sheets(iCurSheet).Range("A2:K1000")
rSearch.AutoFilter Field:=11, Criteria1:="=" & wbMe.Sheets(iCurSheet).Cells(iLine, iColKey).Value
MsgBox "Count Rows rSearch:" & rSearch.Rows.Count

我希望 MsgBox 少说一些,但我得到了 999,所以它没有过滤任何东西。 我猜我过滤了错误的列,但我想过滤 Col K(之后我需要 Col F 再次搜索,抱歉混淆了东西)。

现在我不再收到 AutoFilter 异常。但不知为何,我的研究范围并没有缩小。 如何缩小范围?

【问题讨论】:

  • icolkey 的值是多少?在该代码中它应该是 1。
  • 它是 11。我在 K 列 (11) 中搜索数字。你能解释为什么它必须是 1 吗?
  • 因为您为过滤器指定的范围内只有一列。如果您要过滤 K 列,为什么要将范围设置为 F 列?

标签: excel vba autofilter


【解决方案1】:

计算可见数据单元格(标准单元格)

  • 快速修复可能是这样的

    MsgBox "Count Rows rSearch:" & rSearch.Columns(11).SpecialCells(xlCellTypeVisible).Cells.Count - 1
    
  • 请注意,标头需要包含在 AutoFilter 的范围内才能正常工作。

使用特殊单元格

Sub CountVisibleDataCells()

    ' Define constants.
    Const CriteriaIndex As Long = 11
    
    ' Reference the workbook ('wb').
    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    ' Reference the worksheet ('ws').
    Dim ws As Worksheet: Set ws = wb.Worksheets("Sheet1")
    ' Turn off AutoFilter.
    If ws.AutoFilterMode Then ws.AutoFilterMode = False 
    ' Reference the range ('rg').
    Dim rg As Range: Set rg = ws.Range("A1:K21")
    
    ' Store the criteria value from the cell, converted to a string ('CStr'),
    ' in a string variable ('Criteria'). AutoFilter 'prefers' this.
    Dim Criteria As String: Criteria = CStr(ws.Range("M1").Value)
    
    ' Filter the range.
    rg.AutoFilter Field:=CriteriaIndex, Criteria1:=Criteria
    
    ' Reference the visible cells in the criteria column ('vrg').
    Dim vrg As Range
    Set vrg = rg.Columns(CriteriaIndex).SpecialCells(xlCellTypeVisible)
    
    ' Turn off the worksheet auto filter.
    ws.AutoFilterMode = False
    
    ' Store the number of visible cells of the criteria column
    ' in a long variable (subtract 1 to not count the header).
    Dim CriteriaCount As Long: CriteriaCount = vrg.Cells.Count - 1
    
    ' Inform.
    MsgBox "Count of '" & Criteria & "': " & CriteriaCount

End Sub

使用应用程序计数

Sub CountCriteriaCells()

    ' Define constants.
    Const CriteriaIndex As Long = 11
    
    ' Reference the workbook ('wb').
    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    ' Reference the worksheet ('ws').
    Dim ws As Worksheet: Set ws = wb.Worksheets("Sheet1")
    ' Turn off AutoFilter.
    If ws.AutoFilterMode Then ws.AutoFilterMode = False 
    ' Reference the range ('rg').
    Dim rg As Range: Set rg = ws.Range("A1:K21")
    
    ' Store the criteria value from the cell, converted to a string ('CStr'),
    ' in a string variable ('Criteria').
    Dim Criteria As String: Criteria = CStr(ws.Range("M1").Value)
    ' You may need to modify this because 'CountIf' works differently.
    
    ' Reference the criteria data range ('cdrg') (no headers).
    Dim cdrg As Range
    With rg.Columns(CriteriaIndex)
        Set cdrg = .Resize(.Rows.Count - 1).Offset(1)
    End With
    
    ' Store the number of cells containing the criteria ('CriteriaCount')
    ' in a long variable.
    Dim CriteriaCount As Long
    CriteriaCount = Application.CountIf(cdrg, Criteria)
    
    ' Inform.
    MsgBox "Count of '" & Criteria & "': " & CriteriaCount

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多