【问题标题】:Excel VBA Countif between dates日期之间的Excel VBA Countif
【发布时间】:2014-07-09 02:59:36
【问题描述】:

我需要创建一个函数来计算两个日期之间帐号的出现次数。第一个日期基于函数输入,第二个是提前 3 个月(日期可能不包含在数据集中)。范围内的日期值采用“dd/mm/yyyy h:mm”格式。由于数据集的大小约为 150,000 行,我想在代码中执行此操作,而不是在指定单元格中粘贴或评估 COUNTIF 公式。

工作表函数仅在引用 AccountNo 变量时有效,但在添加条件 ">=" 或 "

例如Application.WorksheetFunction.CountIfs(Range("L2:L" & Endrow), AccountNo) > 1 Then ''''(Works)

函数需要根据countif结果返回一个结果,如下所示。

谢谢,

Function LastWrapUp(Date1 As Date, AccountNo)

Dim Date2 As Date
Dim Endrow As Long

Date2 = DateAdd("M", 3, Date1)
Endrow = Range("A" & Rows.Count).End(xlUp).Row

If Application.WorksheetFunction.CountIfs(Range("A2:A17643"), ">=" & Date1, Range("A2:A" & Endrow), "<" & Date2, Range("L2:L" & Endrow), AccountNo) > 1 Then
    LastWrapUp = "Not Final Wrap Up"
ElseIf Application.WorksheetFunction.CountIfs(Range("A2:A" & Endrow), ">=" & Date1, Range("A2:A" & Endrow), "<" & Date2, Range("L2:L" & Endrow), AccountNo) = 1 Then
    LastWrapUp = "Yes"
Else
    LastWrapUp = "Error"
End If

Debug.Print LastWrapUp

End Function

【问题讨论】:

    标签: function date excel countif vba


    【解决方案1】:

    对于那些可能遇到此问题并感兴趣的人,解决方案是在 Date1 和 Date 2 变量周围添加 Cdbl 和 Cdate 函数(为清楚起见,按照下面的内容更改为 Newdate 和 AccountDate 变量)。奇迹般有效。在 VBA 中使用日期格式可能会很痛苦!

    Function LastWrapUp(AccountID, AccountType, AccountDate As Date)
    
    'Find if current WrapUp is the last for a given account number within a 3 month period.
    'need to include reference to specific sheets
    
    Dim NewDate As Date
    Dim LastRow As Long
    
    NewDate = DateAdd("M", 3, AccountDate)
    
    LastRow = Workbooks("Interim Referrals Report.xlsm").Worksheets("SA Wrap Up Data").Range("A" & Rows.Count).End(xlUp).Row
    
    If AccountType = "Dummy ID" Then
        LastWrapUp = "Dummy ID"
    ElseIf Application.WorksheetFunction.CountIfs(Workbooks("Interim Referrals Report.xlsm").Worksheets("SA Wrap Up Data").Range("A2:A" & LastRow), ">=" & CDbl(CDate(AccountDate)), Workbooks("Interim Referrals Report.xlsm").Worksheets("SA Wrap Up Data").Range("A2:A" & LastRow), "<" & CDbl(CDate(NewDate)), Workbooks("Interim Referrals Report.xlsm").Worksheets("SA Wrap Up Data").Range("L2:L" & LastRow), AccountID) > 1 Then
        LastWrapUp = "Not Final Wrap Up"
    ElseIf Application.WorksheetFunction.CountIfs(Workbooks("Interim Referrals Report.xlsm").Worksheets("SA Wrap Up Data").Range("A2:A" & LastRow), ">=" & CDbl(CDate(AccountDate)), Workbooks("Interim Referrals Report.xlsm").Worksheets("SA Wrap Up Data").Range("A2:A" & LastRow), "<" & CDbl(CDate(NewDate)), Workbooks("Interim Referrals Report.xlsm").Worksheets("SA Wrap Up Data").Range("L2:L" & LastRow), AccountID) = 1 Then
        LastWrapUp = Workbooks("Interim Referrals Report.xlsm").Worksheets("SA Wrap Up Data").Range(AccountID.Address).Offset(0, -4)
    Else
        LastWrapUp = "Error"
    End If
    
    End Function
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-03
    • 2014-06-01
    • 1970-01-01
    • 2017-04-10
    • 1970-01-01
    • 2017-05-28
    • 1970-01-01
    • 2012-12-12
    相关资源
    最近更新 更多