简体   繁体   English

Excel VBA上的日期过滤器

[英]Date filter on Excel VBA

I have searched all over the web and I couldnt find anything that could properly help me with a code. 我在网上搜索了所有内容,但找不到任何可以适当帮助我编写代码的内容。

What I need is simple, but I still couldnt find it. 我需要的很简单,但是我仍然找不到。

*The situation --> Everyday, in my company, we receive a sales report accumulated by the actual month. *情况->每天,在我公司中,我们都会收到按实际月份累积的销售报告。 As I have my own workbook, everyday I gotta filter only the information that I need and paste it on my workbook. 因为我有自己的工作簿,所以每天我只能过滤所需的信息并将其粘贴到我的工作簿中。 When it's monday we receive the information from friday and saturday sales together. 星期一是星期一,我们从星期五和星期六开始收到销售信息。 I'm trying to code something to filter only yesterday, if it's not monday, and to filter friday and saturday, in case it's monday. 我正在尝试编写一些代码,以便仅在昨天(如果不是星期一)进行过滤,并在星期五和星期六进行过滤,以防万一。

COLUMN D Contains the DATE I want to filter 第D栏包含我要过滤的日期

As today is Thursday, I have set the if condition to vbthursday in order to test. 因为今天是星期四,所以我将if条件设置为vbthursday以便进行测试。

sabado = saturday; sabado =星期六; friday = sexta; 星期五= sexta;

Sub Data()
Dim yesterday As String


If Weekday(Date, vbThursday) = 1 Then

Dim sabado As String
Dim sexta As String
Range("D1").Select
Selection.AutoFilter
sabado = Format(Date - 2, "dd/mm/yyyy")
sexta = Format(Date - 3, "dd/mm/yyyy")

ActiveSheet.Range("$A$1:$E$11").AutoFilter Field:=4, Operator:= _
xlFilterValues, Criteria1:="=" & sabado, Criteria2:="=" & sexta



Else

Range("D1").Select
Selection.AutoFilter
yesterday = Format(Date - 1, "dd/mm/yyyy")
ActiveSheet.Range("$A$1:$E$11").AutoFilter Field:=4, Operator:= _
xlFilterValues, Criteria1:="=" & yesterday



End If

Actualy the if condition is working, it recognizes when it's vbMonday (in the test case, vbThursday) and when it's not. 实际上,if条件在起作用,它可以识别何时为vbMonday(在测试案例中为vbThursday),何时不工作。 The else condition is also working too and it filters just as it should filter. else条件也可以正常工作,它会像应该过滤一样进行过滤。 The problem is that, when the macro enters the if condition, it does not filter properly. 问题是,当宏进入if条件时,它不能正确过滤。 It runs throught the end and does not accuse any errors. 它一直运行到最后,并且没有任何错误。

Somebody help :( 有人帮助:(

How about something like this? 这样的事情怎么样?

Sub Data()
    Dim datesToFilter()

    If Weekday(Date, vbMonday) = 1 Then
        datesToFilter = Array(Date - 2, Date - 3)
    Else
        datesToFilter = Array(Date - 1)
    End If

    ActiveSheet.Range("$A$1:$E$11").AutoFilter Field:=4, Operator:= _
    xlFilterValues, Criteria1:=datesToFilter

End Sub

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

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