+ Reply to Thread
Results 1 to 6 of 6

Function to detrmine start and end of month

Hybrid View

  1. #1
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Function to detrmine start and end of month

    Hello everybody
    I need your help to make this function work
    Function NewMonth(sdate As Date)
        If sdate >= CDate(DateSerial(Year(sdate), 12, 17)) And CDate(sdate <= DateSerial(Year(sdate), 1, 16)) Then
            NewMonth = "January"
        End If
    End Function
    I tested it but I got no result
    I want to check a date in a cell and if this date is greater than 17 Dec. and less than 16 Jan. the result should be "January"

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,968

    Re: Function to detrmine start and end of month

    The date can't be greater than 17 Dec and less than 16 Jan of the same year!
    Everyone who confuses correlation and causation ends up dead.

  3. #3
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Function to detrmine start and end of month

    So how it can be fixed? I tried
    Year(sdate)-1
    but no vain

  4. #4
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Function to detrmine start and end of month

    I tried this function but I got incorrect results at some dates
    Function NewMonth(sdate As Date)
            If IsEmpty(sdate) Or sdate = 0 Then
                NewMonth = ""
            ElseIf sdate <= DateSerial(Year(sdate) - 1, 12, 17) And sdate >= DateSerial(Year(sdate) + 1, 1, 16) Then
                NewMonth = "January"
            ElseIf sdate >= DateSerial(Year(sdate), 12, 16) And sdate <= DateSerial(Year(sdate), 11, 17) Then
                NewMonth = "February"
            ElseIf sdate >= DateSerial(Year(sdate), 2, 17) And sdate <= DateSerial(Year(sdate), 3, 16) Then
                NewMonth = "March"
            ElseIf sdate >= DateSerial(Year(sdate), 3, 17) And sdate <= DateSerial(Year(sdate), 4, 16) Then
                NewMonth = "April"
            ElseIf sdate >= DateSerial(Year(sdate), 4, 17) And sdate <= DateSerial(Year(sdate), 5, 16) Then
                NewMonth = "May"
            ElseIf sdate >= DateSerial(Year(sdate), 5, 17) And sdate <= DateSerial(Year(sdate), 6, 16) Then
                NewMonth = "June"
            ElseIf sdate >= DateSerial(Year(sdate), 6, 17) And sdate <= DateSerial(Year(sdate), 7, 16) Then
                NewMonth = "July"
            ElseIf sdate >= DateSerial(Year(sdate), 7, 17) And sdate <= DateSerial(Year(sdate), 8, 16) Then
                NewMonth = "August"
            ElseIf sdate >= DateSerial(Year(sdate), 8, 17) And sdate <= DateSerial(Year(sdate), 9, 16) Then
                NewMonth = "September"
            ElseIf sdate >= DateSerial(Year(sdate), 9, 17) And sdate <= DateSerial(Year(sdate), 10, 16) Then
                NewMonth = "October"
            ElseIf sdate >= DateSerial(Year(sdate), 10, 17) And sdate <= DateSerial(Year(sdate), 11, 16) Then
                NewMonth = "Novermber"
            ElseIf sdate >= DateSerial(Year(sdate) + 1, 11, 17) And sdate <= DateSerial(Year(sdate) - 1, 1, 16) Then
                NewMonth = "December"
            End If
    End Function

  5. #5
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,968

    Re: Function to detrmine start and end of month

    Your code isn't entirely clear, but assuming any day after 17 should belong to the following month, try this:
    Function NewMonth(sdate As Date)
        If IsEmpty(sdate) Or sdate = 0 Then
            NewMonth = ""
        Else
            NewMonth = Format(DateAdd("m", -(Day(sdate) > 16), sdate), "mmmm")
        End If
    End Function

  6. #6
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Function to detrmine start and end of month

    Really wonderful , fantastic, amazing
    I can't imagine it would be so easy like that
    You are perfect Mr. romperstomper

+ 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. [SOLVED] Date function- 1st day of month for 2nd full month from start date
    By vidiotdave in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 07-25-2014, 05:33 AM
  2. Replies: 6
    Last Post: 03-03-2013, 01:41 AM
  3. Replies: 8
    Last Post: 03-29-2010, 09:20 PM
  4. Start month, end month and cost.
    By Ainsley in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 11-16-2007, 08:41 AM
  5. Detrmine How Many Rows are Frozen At Top Of Worksheet
    By Mariano42 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-07-2007, 05:21 PM

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