简体   繁体   English

函数问题:Excel VBA 中的日期过滤器

[英]Function Issue: Date filter in Excel VBA

So, I'm having an issue with making a function that just returns 1 if the date is set in a quarter previous to the actual one.所以,如果日期设置在实际日期之前的四分之一,我在制作一个只返回 1 的函数时遇到了问题。 I'm not fluent with functions, so it might be a stupid mistake, but I can't fix it.我不熟悉函数,所以这可能是一个愚蠢的错误,但我无法修复它。 Thank you kind souls:)谢谢好心人:)

Public Function IsOnQX(ByVal InputCell As Range, ByVal Month As Long) As Integer
    Dim ActualQuarter As String
    Dim PastQuarter As String

        If (Month < 1) Or (Month > 12) Then
                Call Err.Raise(6) ' Value out of Bounds '
        End If

        Set ActualQuarter = ((Month(Now())) + 2) \ 3
        Set PastQuarter = ((Month(InputCell.Value)) + 2) / 3

        If (InputCell.Value = "") Or (PastQuarter < ActualQuarter) Then
            IsOnQX = 1
        Else
            IsOnQX = 0
        End If

End Function

My idea is to use this function as part of a COUNTIFS statement in a Macro that returns the number of items that comply with two conditions: either the cell in the table is empty (returns 1) or the date contained in said cell is from a previous quarter period (=1) again, and just count the 1s to get the total amount.我的想法是将此函数用作宏中 COUNTIFS 语句的一部分,该语句返回符合两个条件的项目数:表中的单元格为空(返回 1)或所述单元格中包含的日期来自上一季度期间 (=1) 再次计数,只需计算 1 即可得出总数。

Something like;就像是;

Range("C63")=COUNTIFS(TablaI_R[Project Name], IF(ISNUMBER(SEARCH(TablaI_R[Project Name],AZAR),1,0)=1), TablaI_R[Actual Closed Date], IF(IsOnQX(TablaI_R[Actual Closed Date])=1, 1, 0)=1)

Thanks everyone!感谢大家!

You shouldn't declare String variables when doing simple arithmetic.在进行简单算术运算时,不应声明String变量。
Also Set is used for Object type variables. Set也用于Object类型变量。

You could use Let for other variables, but that isn't needed in modern Visual Basic.您可以将Let用于其他变量,但在现代Visual Basic 中不需要这样做。

Public Function IsOnQX(ByVal InputCell As Range, ByVal Month As Long) As Integer
    Dim ActualQuarter As Integer
    Dim PastQuarter As Integer
    If (Month < 1) Or (Month > 12) Then
            Call Err.Raise(6) ' Value out of Bounds '
    End If

    ActualQuarter = (Month(Date) + 2) \ 3
    PastQuarter = (Month(InputCell.Value) + 2) \ 3
    If IsEmpty(InputCell) Or (PastQuarter < ActualQuarter) Then
        IsOnQX = 1
    Else
        IsOnQX = 0
    End If
End Function

Edit: Updated to return a 1 for a blank cell编辑:更新为空白单元格返回1

Given a date, you can do this with a one-liner:给定一个日期,你可以用一行代码来做到这一点:

Function inPrevQx(D As Date) As Long
    inPrevQx = IIf(Abs(DateDiff("m", D, Date)) > 6 And CLng(D) <> 0, 0, Abs(DatePart("q", DateAdd("q", 1, D)) = DatePart("q", DateAdd("q", 0, Date)) Or CLng(D) = 0))
End Function

在此处输入图像描述

By examining the formula, and using VBA help to understand the functions, you should be able to figure out the algorithm.通过检查公式并使用 VBA 帮助理解函数,您应该能够弄清楚算法。 Ask if you have any questions.询问您是否有任何问题。

Edit 2编辑 2

OP has clarified that he wants a 0 returned for the current quarter, and a 1 returned for any other quarter (or blanks, I assume). OP 已经澄清,他希望当前季度返回0 ,任何其他季度返回1 (我假设是空白)。

That being the case, the above can be modified to:既然如此,上面可以修改为:

Edit 3 (further simplification)编辑 3 (进一步简化)

Function notInCurrQx(D As Date)
  notInCurrQx = Abs(DateDiff("q", D, Date) <> 0)
  'or even:  notInCurrQx = -(DateDiff("q", D, Date) <> 0)
End Function

在此处输入图像描述

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

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