开发者

Code for Continuous week and quarter count

I am creating a dashboard 开发者_Python百科but I need to make the date's week and quarter continuous and automatic. Anyone hre who can help with the code?

I did try making it manually but then my client requires me to have it automatically. I am using excel vba


You can use my Week and Quarter functions:

' Returns the ISO 8601 week of a date.
' The related ISO year is returned by ref.
'
' 2016-01-06. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function Week( _
    ByVal Date1 As Date, _
    Optional ByRef IsoYear As Integer) _
    As Integer

    Dim Month       As Integer
    Dim Interval    As String
    Dim Result      As Integer
    
    Interval = IntervalSetting(dtWeek)
    
    Month = VBA.Month(Date1)
    ' Initially, set the ISO year to the calendar year.
    IsoYear = VBA.Year(Date1)
    
    Result = DatePart(Interval, Date1, vbMonday, vbFirstFourDays)
    If Result = MaxWeekValue Then
        If DatePart(Interval, DateAdd(Interval, 1, Date1), vbMonday, vbFirstFourDays) = MinWeekValue Then
            ' OK. The next week is the first week of the following year.
        Else
            ' This is really the first week of the next ISO year.
            ' Correct for DatePart bug.
            Result = MinWeekValue
        End If
    End If
        
    ' Adjust year where week number belongs to next or previous year.
    If Month = MinMonthValue Then
        If Result >= MaxWeekValue - 1 Then
            ' This is an early date of January belonging to the last week of the previous ISO year.
            IsoYear = IsoYear - 1
        End If
    ElseIf Month = MaxMonthValue Then
        If Result = MinWeekValue Then
            ' This is a late date of December belonging to the first week of the next ISO year.
            IsoYear = IsoYear + 1
        End If
    End If
    
    ' IsoYear is returned by reference.
    Week = Result
        
End Function
' Returns the quarter of a date.
' Value is 1 for the first quarter of the year,
' 4 for the last.
'
' 2015-12-21. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function Quarter( _
    ByVal Date1 As Date) _
    As Integer
    
    Dim Result  As Integer
    
    ' Find the quarter.
    Result = DatePart(IntervalSetting(DtInterval.dtQuarter), Date1)
    
    Quarter = Result
    
End Function

They both use several constants and supporting functions. Too much to post here, so please visit the full code and documentation at my project at GitHub: VBA.Date.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜