【问题标题】:Need to count days open in a month based on multiple openings and closings需要根据多个开放和关闭来计算一个月中的开放天数
【发布时间】:2021-02-26 23:07:14
【问题描述】:

我正在尝试根据多次重新开放和重新关闭来计算给定位置在一个月内开放的天数。

【问题讨论】:

  • 我建议您为此使用 VBA。也许是一个 UDF。

标签: excel days


【解决方案1】:

UDF1

这是一个用户定义函数的例子:

Function FunDaysOfMonth(RngFrom As Range, RngTo As Range, RngIn As Range)
    
    'Declarations.
    Dim DatDate01 As Date
    Dim DatDate02 As Date
    
    'Setting DatDate01 as the later betweeen RngFrom.Value and the first day of the month of RngIn.Value.
    DatDate01 = Excel.WorksheetFunction.Max(RngFrom.Value, _
                                            RngIn.Value - DateTime.Day(RngIn.Value) + 1 _
                                           )
    
    'Setting DatDate01 as the earlier betweeen RngTo.Value and the last day of the month of RngIn.Value.
    DatDate02 = Excel.WorksheetFunction.Min(RngTo.Value, _
                                            DateAdd("m", 1, RngIn.Value) - DateTime.Day(RngIn.Value) _
                                           )
    'Setting FunFaysOfMonth.
    FunDaysOfMonth = Excel.WorksheetFunction.Max(DatDate02 - DatDate01 + 1, 0)
    
End Function

将它放在工作簿的一个模块中。然后你可以在单元格 L4 中写一个公式,如

=FunDaysOfMonth($D4,$E4,L$3)+FunDaysOfMonth($F4,$G4,L$3)+FunDaysOfMonth($H4,$I4,L$3)

UDF2

您还可以使用更复杂的用户定义函数,例如:

Function FunDaysOfMonthWithRanges(RngFrom As Range, RngTo As Range, RngIn As Range)
    
    'Declarations.
    Dim DatDate01 As Date
    Dim DatDate02 As Date
    Dim DblDateCount As Double
    
    'Checking if the cells count of RngFrom is equal to the one of RngTo.
    If RngFrom.Cells.Count <> RngFrom.Cells.Count Then
        
        'Setting FunDaysOfMonthWithRanges.
        FunDaysOfMonthWithRanges = "#DateMissing"
        Exit Function
        
    End If
    
    'Covering each couple of cells.
    For DblDateCount = 1 To RngFrom.Cells.Count
        
        'Checking if there is a mismatch between the two dates in the given cells of RngFrom and RngTo.
        If Excel.WorksheetFunction.Small(RngFrom, DblDateCount) > Excel.WorksheetFunction.Small(RngTo, DblDateCount) Then
            FunDaysOfMonthWithRanges = "#DateMismatch"
            Exit Function
        End If
        
        'Setting DatDate01 as the later betweeen the value of the given cell of RngFrom and the first day of the month of RngIn.Value.
        DatDate01 = Excel.WorksheetFunction.Max(Excel.WorksheetFunction.Small(RngFrom, DblDateCount), _
                                                RngIn.Value - DateTime.Day(RngIn.Value) + 1, _
                                                DatDate01 _
                                               )
        
        'Setting DatDate01 as the earlier betweeen the value of the given cell of RngTo and the last day of the month of RngIn.Value.
        DatDate02 = Excel.WorksheetFunction.Min(Excel.WorksheetFunction.Small(RngTo, DblDateCount), _
                                                DateAdd("m", 1, RngIn.Value) - DateTime.Day(RngIn.Value) _
                                               )
        
        'Setting FunDaysOfMonthWithRanges.
        FunDaysOfMonthWithRanges = FunDaysOfMonthWithRanges + Excel.WorksheetFunction.Max(DatDate02 - DatDate01 + 1, 0)
        
    Next
    
End Function

有了这个,您可以选择具有多个开店和关店的范围(同一家商店)。单元格 L4 中的公式如下所示:

=FunDaysOfMonthWithRanges(($D4,$F4,$H4),($E4,$G4,$I4),L$3)

注意

在这两种情况下,都必须正确报告日期。不会注意到开口重叠。例如:从 2021 年 2 月 2 日到 2021 年 4 月 2 日的一个开放时间和 2021 年 4 月 2 日和 2021 年 7 月 2 日之间的另一个开放时间重叠了一天(04/02/2021),这将被计为额外天。重叠 2 天,将额外计算 2 天。

【讨论】:

    猜你喜欢
    • 2011-07-13
    • 1970-01-01
    • 1970-01-01
    • 2011-09-06
    • 1970-01-01
    • 2018-04-12
    • 2014-08-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多