需要根据多次开门和关门计算一个月内的开门天数

问题描述

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

enter image description here

解决方法

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,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,_
                                                DateAdd("m",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,$I4),L$3)

注意

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...