简体   繁体   English

自动过滤后应用高级过滤器

[英]Apply advanced filter after autofilter

I want to do two successive filtering; 我想做两次连续过滤; the first on dates which I use auto-filter and on the produced result I want to do advance-filter (because I have OR in mind). 第一个在我使用自动过滤器的日期和生成的结果我想做进行过滤(因为我记住了OR)。 So what I did first was to set a range variable to the unfiltered range. 所以我首先要做的是将范围变量设置为未过滤范围。

Set rng = Range(ws.Cells(1, 1), ws.Cells(rowNos, colNos))

Then using auto-filter I filter for given dates. 然后使用自动过滤器I过滤给定日期。

rng.AutoFilter Field:=1, Criteria1:=">" & lDate

Since now some rows will be hidden, and I want to apply advanced filter, I made use of specialcells 从现在开始会隐藏一些行,而我想应用高级过滤器,我使用了specialcells

rng.SpecialCells(xlCellTypeVisible).AdvancedFilter Action:=xlFilterCopy, _
    CriteriaRange:=crt, CopyToRange:=thisWB.Worksheets("Sheet3").Range("A1"), _
    Unique:=False

However I got an error in the last step "the command requires at least two rowa of data . . " I made sure that there were at least 100 rows which fit the criteria meaning that the error is not because of a lack of rows. 但是我在最后一步得到了一个错误“命令需要至少两行数据。”我确保至少有100行符合标准,这意味着错误不是因为缺少行。

Please help me get the problem. 请帮我解决问题。 Also if there's another way I can accomplish the task I'll be happy to change my codes. 此外,如果有另一种方式我可以完成任务,我将很乐意改变我的代码。 What I'm trying to do is for specific dates filter a table and then filter again for values on two columns (as is normally done with advanced filters). 我要做的是针对特定日期过滤表格,然后再次过滤两列上的值(通常使用高级过滤器完成)。

It seems like .AdvancedFilter does not work on non-continuous ranges. 似乎.AdvancedFilter不适用于非连续范围。 The code below is a bit kludge-y, but worked for a little example I pulled together where I want to return observations that are > April 1st, 2014 where Foo = Yes and Bar = 7. My data sheet contains exactly one row that matches all those criteria. 下面的代码有点像kludge-y,但是我在一个小例子中工作,我想要返回的结果是> 2014年4月1日,其中Foo = Yes和Bar = 7.我的数据表只包含一行匹配所有这些标准。

设定

Option Explicit
Sub FilterTwice()

Dim DataSheet As Worksheet, TargetSheet As Worksheet, _
    ControlSheet As Worksheet, TempSheet As Worksheet
Dim DataRng As Range, ControlRng As Range, _
    TempRng As Range
Dim lDate As Date
Dim LastRow As Long, LastCol As Long

'assign sheets for easy reference
Set DataSheet = ThisWorkbook.Worksheets("Sheet1")
Set ControlSheet = ThisWorkbook.Worksheets("Sheet2")
Set TargetSheet = ThisWorkbook.Worksheets("Sheet3")

'clear any previously-set filters
Call ClearAllFilters(DataSheet)

'assign data range
LastRow = DataSheet.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastCol = DataSheet.Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
Set DataRng = Range(DataSheet.Cells(1, 1), DataSheet.Cells(LastRow, LastCol))

'assign a control (or critieria) range for the advanced filter
Set ControlRng = Range(ControlSheet.Cells(1, 1), ControlSheet.Cells(2, 2))

'apply date filter
lDate = "4/1/2014"
With DataRng
    .AutoFilter Field:=1, Criteria1:=">" & lDate
End With

'add a temporary sheet and copy the visible cells to create a continuous range
Set TempSheet = Worksheets.Add
DataRng.SpecialCells(xlCellTypeVisible).Copy
TempSheet.Range("A1").PasteSpecial Paste:=xlPasteAll

'assign temp range
LastRow = TempSheet.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastCol = TempSheet.Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
Set TempRng = Range(TempSheet.Cells(1, 1), TempSheet.Cells(LastRow, LastCol))

'apply advanced filter to temp range and get obs where foo = yes and bar = 7
With TempRng
    .AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=ControlRng, _
        CopyToRange:=TargetSheet.Range("A1"), Unique:=False
End With

'remove the temp sheet and clear filters on the data sheet
Application.DisplayAlerts = False
TempSheet.Delete
Application.DisplayAlerts = True
DataSheet.AutoFilterMode = False

End Sub

Sub ClearAllFilters(cafSheet As Worksheet)
    With cafSheet
        .AutoFilterMode = False
        If .FilterMode = True Then
            .ShowAllData
        End If
    End With
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Excel Advanced筛选器运行速度很慢,但是仅在运行自动筛选器之后 - Excel Advanced Filter Very slow to run, but only after autofilter has been run Macro 中的 AutoFilter 只适用于我 go 进入 Excel 运行后打开过滤器选项并单击确定 - AutoFilter in Macro does only apply when I go into Excel after running it open the filter options and click ok 使用高级筛选器单元格的范围作为另一张纸上自动筛选的条件 - Use Range of Advanced Filter Cells as Criteria for AutoFilter on Another Sheet 根据第一张纸上的过滤器在工作表上应用动态自动筛选 - Apply dynamic AutoFilter across worksheets based on filter on 1st sheet 在工作表计算后使用宏应用自动过滤器? - Apply autofilter using macro after worksheet has calculated? 使用高级筛选器进行筛选复制后表格范围未调整大小 - Table Range not resizing after FilterCopy with Advanced Filter .ShowAllData 高级筛选后,表未完全“清除” - .ShowAllData after Advanced Filter, Table not fully "clearing" 尝试使用范围作为对话框的输入来应用高级过滤器? - Trying to apply advanced filter using range as input from a dialog box? 当报告的标题出现在不同的行时应用高级过滤器 - Apply advanced filter when report's headers come on different rows 使用 Python 将自动过滤器应用于工作表 - Apply Autofilter to worksheet using Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM