+ Reply to Thread
Results 1 to 2 of 2

Break-down required of UDF-function: fGetFirstButtonDate

Hybrid View

  1. #1
    Registered User
    Join Date
    05-08-2013
    Location
    Acht, NL
    MS-Off Ver
    Excel 2016
    Posts
    26

    Cool Break-down required of UDF-function: fGetFirstButtonDate

    Hi,

    Since Excel2003 or Excel2007 (I am not sure) there is no calendar function anymore.
    So the last few years I have been trying to, somehow, recreate that calendar.
    The one I am using now works perfectly, but I need some help explaning a piece of code I retained from somewhere(?), but I can't break it down (so to speak).

    I am using the following UDF-functions in VBA Excel2016:
    Public Function fGetFirstButtonDate(p_dtThisDate As Date) As Date
    'Calculate the first DAYnumber from a specific date as starting point.
    '==> Param [p_dtThisDate] = current date OR selected month/year with current daynumber.
        
        fGetFirstButtonDate = p_dtThisDate - ((Day(p_dtThisDate) \ 7 + IIf(Day(p_dtThisDate) Mod 7 = 0, 0, 1)) * 7 + Format(p_dtThisDate, "w", vbTuesday))
    
    End Function
    Public Function fGetTotDaysOfMonth(p_dtInitDate As Date, p_iFactor As Integer) As String
    'Determine the total days of a specific month.
    '==> Param [p_dtInitDate] = specific date.
    
        fGetTotDaysOfMonth = Day(DateSerial(Year(p_dtInitDate), Month(p_dtInitDate) + p_iFactor, 1) - 1)
    
    End Function
    This function is triggered by the following piece of code (see code in RED):
    Private Sub NewMonth()
    'The month of the selected date ("frmCalendar.Tag") is shown with weeknumbers.
    
        Dim dtThisDate     As Date
        Dim dtButtonDate   As Date
        Dim dtFirstWeekDay As Date
        Dim sTotDaysMonth  As String
        Dim bNotFirstPass  As Boolean
    
        bNotFirstPass = False
        With frmCalendar
            'First letter of the current month is written with a capitol.
            .lblNavMonthYear.Caption = WorksheetFunction.Proper(Format(.Tag, "mmmm  yyyy"))
            'Assign todays date to variable.
            dtThisDate = CDate(.Tag)
        End With
        Dim i As Integer
        For i = 0 To 41
            'When first weekday number has to be calculated, then continue.
            If bNotFirstPass = False Then
                'GoTo FUNC: [Calculate the first DAYnumber from a specific date as starting point].
                dtButtonDate = fGetFirstButtonDate(dtThisDate)
                'GoTo FUNC: [Determine the total days of a specific month].
                sTotDaysMonth = fGetTotDaysOfMonth(dtThisDate, 0)
                'First calendar row MUST contain at least ONE day of the selected month.
                If Day(dtButtonDate) < (sTotDaysMonth - 6) Or Day(dtButtonDate + 6) = sTotDaysMonth Then
                     dtButtonDate = (dtButtonDate + 7)
                End If
                dtFirstWeekDay = dtButtonDate
                bNotFirstPass = True
            Else
                dtButtonDate = (dtButtonDate + 1)
            End If
            'Calculate the WEEK-numbers on the first passthrough.
            If i = 0 Then
                Dim j As Integer
                For j = 0 To 5
                    'GoTo FUNC: [Calculate [weeknumber] from a specific date as starting point].
                    frmCalendar("lblWeek" & j + 1).Caption = fGetWeekNumber(dtFirstWeekDay)
                    dtFirstWeekDay = (dtFirstWeekDay + 7)
                Next
            End If
            'All the 'day-buttons' are getting a DAY-number.
            With frmCalendar("lblDay" & i + 1)
                .Caption = Format(dtButtonDate, "d")
                'GoTo FUNC: [Calculate [weeknumber] from a specific date as starting point].
                'If WEEK-number of current date equals WEEK-number of the first day of the calendar,...
                If fGetWeekNumber(Date) = fGetWeekNumber(dtButtonDate) _
                   And Year(Date) = Year(dtThisDate) Then
                    '...then BackStyle and BorderStyle are DARK-GREY(&H00DDDDDD&).
                    .BackStyle = fmBackStyleOpaque
                    .BorderStyle = fmBorderStyleSingle
                Else
                    '...else BackStyle and BorderStyle LIGHT-GREY [=background] (&H80000004&).
                    .BackStyle = fmBackStyleTransparent
                    .BorderStyle = fmBorderStyleNone
                End If
                'Establish whether it's a WEEK-day or a WEEKEND-day.
                If (i = 5 Or i = 6) Or _
                   (i = 12 Or i = 13) Or _
                   (i = 19 Or i = 20) Or _
                   (i = 26 Or i = 27) Or _
                   (i = 33 Or i = 34) Or _
                   (i = 40 Or i = 41) Then
                    'In case of a WEEKEND-day (sat/sun), then DAY-number is BOLD.
                    .Font.Bold = True
                    'In case the MONTH-number of the button equals the MONTH-number of current date,...
                    '...then Fontcolor is BLUE (&H00D77800&) and ELSE it's GREY (&H00AFB9BC&).
                    .ForeColor = IIf(Month(dtButtonDate) = Month(dtThisDate), 14120960, 11516348)
                    'In case the MONTH-number of the button equals the MONTH-number of current date AND...
                    '...DAY-number on the button equals DAY-number of current date, ....
                    If Month(dtButtonDate) = Month(dtThisDate) Then
                        '...then BorderColor = DARK-RED (&H000000C0&)and ELSE it's LIGHT-GREY [=background] (&H80000004&).
                        .BorderColor = IIf(dtButtonDate = Date, 192, -2147483644)
                        '...and FontColor = DARK-RED (&H000000C0&)and ELSE it's BLUE (&H00D77800&).
                        .ForeColor = IIf(dtButtonDate = Date, 192, 14120960)
                    Else
                        '...else BorderColor = LIGHT-GREY [background] (&H80000004&)...
                        .BorderColor = -2147483644
                        '...and FontColor = GREY (&H00AFB9BC&)
                        .ForeColor = 11516348
                    End If
                Else
                    'In case of a WEEK-day (mon/fri), then DAY-number is REGULAR (not bold).
                    .Font.Bold = False
                    'In case the MONTH-number of the button equals the MONTH-number of current date,...
                    '...then Fontcolor is ANTRACITE-BLACK (&H00404040&) and ELSE it's GREY (&H00AFB9BC&).
                    .ForeColor = IIf(Month(dtButtonDate) = Month(dtThisDate), 4210752, 11516348)
                    'In case the MONTH-number of the button equals the MONTH-number of current date AND...
                    '...DAY-number on the button equals DAY-number of current date, ....
                    If Month(dtButtonDate) = Month(dtThisDate) Then
                        '...then BorderColor = DARK-RED (&H000000C0&)and ELSE it's LIGHT-GREY [=background] (&H80000004&).
                        .BorderColor = IIf(dtButtonDate = Date, 192, -2147483644)
                        '...and FontColor = DARK-RED (&H000000C0&)and ELSE it's ANTRACITE-BLACK (&H00404040&).
                        .ForeColor = IIf(dtButtonDate = Date, 192, 4210752)
                    Else
                        '...else BorderColor = LIGHT-GREY [background] (&H80000004&)...
                        .BorderColor = -2147483644
                        '...and FontColor = GREY (&H00AFB9BC&)
                        .ForeColor = 11516348
                    End If
                End If
            End With
        Next
        'GoTo PROC: [Establish the (in)visibility of the [Today]-button].
        Call TodayButtonVisibility
    
    End Sub
    My question revolves around this piece of code: ((Day(p_dtThisDate) \ 7 + IIf(Day(p_dtThisDate) Mod 7 = 0, 0, 1)) * 7 + Format(p_dtThisDate, "w", vbTuesday))

    If the date is set to 17-01-2022 (January 17th) then the following values are 'calculated':

    VBA - Controls.jpg

    It ends in a calendar that looks like this:

    Calendar.jpg

    If someone could help me out, that would be awesome!
    Last edited by Irmaxx; 02-05-2022 at 11:18 AM. Reason: Bad grammar...
    Win10Pro 22H2 | Excel2016

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2406 Win 11 Home 64 Bit
    Posts
    23,982

    Re: Explanation required

    Administrative Note:

    Welcome to the forum.

    We would very much like to help you with your query, however the thread title does not really convey what your request is about. Tell us what you are trying to do, not how you think it should be done.

    Please take a moment to amend your thread title. Make sure that the title properly explains your request. Your title should be explicit and not be generic (this includes function names used without an indication of what you are trying to achieve).

    Please see Forum Rule #1 about proper thread titles and adjust accordingly. To edit the thread title, open the original post to edit and then click on Go Advanced (bottom right) to access the area where you can edit your title.

    (Note: this change is not optional. No help to be offered until this moderation request has been fulfilled.)
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. My first try with loops...but it is not working... explanation required!
    By Alex Piotto in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-11-2018, 12:22 PM
  2. Testing if an object exists... explanation required
    By anrichards22 in forum Excel Programming / VBA / Macros
    Replies: 23
    Last Post: 12-28-2017, 06:10 AM
  3. [SOLVED] explanation required for HD serial no same on 3 different pc
    By JEAN1972 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-26-2017, 10:06 AM
  4. VBA explanation required
    By ntljennifer in forum Excel General
    Replies: 2
    Last Post: 06-27-2014, 09:05 PM
  5. Multi Level Dependent Drop-down list, Explanation required
    By Shoieb.arshad in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 01-31-2012, 11:36 AM
  6. A better explanation of help required
    By Elvey in forum Excel General
    Replies: 3
    Last Post: 05-20-2006, 12:28 PM
  7. Simple explanation of formula required pls
    By Swn-Y-Mor in forum Excel General
    Replies: 4
    Last Post: 01-14-2005, 08:48 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1