简体   繁体   English

日期筛选器通过Excel VBA隐藏所有单元格

[英]Date Filter Hiding All Cells Through Excel VBA

I'm using the below to filter out specific dates however it hides all cells and does not leave in the required Data. 我正在使用以下内容过滤掉特定的日期,但是它隐藏了所有单元格,并且没有保留在所需的数据中。 When completed manually it works fine it also works when I change the range to the last row( $AL1210 ) however this range will increase as I will be pasting in new data daily. 手动完成后,它可以正常工作,当我将范围更改为最后一行( $ AL1210 )时,它也可以工作,但是由于我每天都会粘贴新数据,因此该范围会增加。

I am trying to find a solution for the range to ideally automatically increase to last row(although I've tried this from other answers and get the same result, as I don't understand why the filter does not work, can anyone help me with this? 我正在尝试找到一种理想情况下自动增加到最后一行的范围的解决方案(尽管我已经尝试了其他答案并获得了相同的结果,因为我不明白为什么过滤器不起作用,有人可以帮助我吗?有了这个?

Sub Auto_Filter()
    `Auto_Filter Macro
    `This Auto Filters all data for years 2017 & 2018, this also sorts to newest date first
    Keyboard Shortcut: Ctrl+Shift+A
    Sheets("Paste Data").Select
    ActiveSheet.Range("$A$1:$AL$10000").AutoFilter Field:=9, Criteria1:= _
        ">=01/01/2017", Operator:=xlAnd, Criteria2:="<=31/12/2018"
    ActiveWorkbook.Worksheets("Paste Data").AutoFilter.Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Paste Data").AutoFilter.Sort.SortFields.Add Key:= _
        Range("I1:I10000"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption _
        :=xlSortNormal
    With ActiveWorkbook.Worksheets("Paste Data").AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub

If you wanted to set the range dynamically. 如果要动态设置范围。

Dim rng As Range, sh As Worksheet
Set sh = Sheets("Paste Data")
With sh
    Set rng = .Range("A1:AL" & .Cells(.Rows.Count, "AL").End(xlUp).Row)

End With

use "yyyy/mm/dd" date format in your code – JK2017 在代码中使用“ yyyy / mm / dd”日期格式– JK2017

Works Thanks 作品谢谢

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM