Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Find days in a quarter in a date range

My worksheet looks like this

enter image description here

I was able to get the total quarters between a date range as shown in cell D4. I created a UDF and it works well.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Function GetDayPerQtr(ReimDt As Date, HoldDt As Date) As String
    Dim Qtr As Integer
    Dim QtrStDt As Date
    Dim QtrEndDate As Date
    Dim TotalQtrs As Long
    Dim tmp As String
    Dim i As Long
    
    Qtr = DetermineQuarter(ReimDt)
    QtrEndDate = DateSerial(Year(ReimDt), (Qtr * 3) + 1, 0)
    
    If QtrEndDate <= HoldDt Then
        TotalQtrs = DateDiff("q", ReimDt, HoldDt)
        
        If TotalQtrs > 1 Then
            QtrStDt = ReimDt
            
            For i = 1 To TotalQtrs
                Qtr = DetermineQuarter(QtrStDt)
                If Qtr = 2 Or Qtr = 3 Then
                    QtrEndDate = DateSerial(Year(QtrStDt), Qtr * 3, 30)
                Else
                    QtrEndDate = DateSerial(Year(QtrStDt), Qtr * 3, 31)
                End If
                
                tmp = tmp & ", " & (QtrEndDate - QtrStDt + 1)
                QtrStDt = QtrEndDate + 1
            Next i
            
            tmp = tmp & ", " & (HoldDt - QtrStDt + 1)
            tmp = Mid(tmp, 2)
        Else
            tmp = QtrEndDate - ReimDt
            QtrStDt = QtrEndDate + 1
            tmp = tmp & ", " & (HoldDt - QtrStDt + 1)
        End If
    Else
        tmp = HoldDt - ReimDt
    End If
    
    GetDayPerQtr = tmp
End Function

Function DetermineQuarter(Dt As Date) As Integer
    Dim inputDate As Date
    Dim quarter As Integer
    Dim inputMonth As Integer
    
    inputDate = Dt
    inputMonth = Month(inputDate)
    
    If inputMonth >= 1 And inputMonth <= 3 Then
        quarter = 1
    ElseIf inputMonth >= 4 And inputMonth <= 6 Then
        quarter = 2
    ElseIf inputMonth >= 7 And inputMonth <= 9 Then
        quarter = 3
    ElseIf inputMonth >= 10 And inputMonth <= 12 Then
        quarter = 4
    End If
    DetermineQuarter = quarter
End Function

Now I am trying to get the result as shown in green cells based on the start date and end date.

So basically, the values which I am showing in cell D4 should go into respective columns.

I tried various combination of the above code and ended up getting confused. Can someone help me or guide me how to achieve what I want? I am open to both VBA and Excel Formula suggestions.

I also tried to incorporate a logic around the below but got stuck.

'=GetTotalDaysInQtr("Q1",2020,B4,C4)
Function GetTotalDaysInQtr(Qtr As String, Yr As Long, ReimDt As Date, HoldDt As Date) As Long

End Function

>Solution :

If you put true dates on the sheet you can use a formula like this:

=IF(AND($B3>=M$2,$A3<=N$2),MIN(N$2,$B3)-MAX(M$2,$A3),0)

enter image description here

Creating the quarter dates can be achieved with the formula as well using the EDATE function with a start date like 1.1.2020 and a months parameter of 3.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading