Consulting

Results 1 to 7 of 7

Thread: Return Last April using VBA

  1. #1
    VBAX Newbie
    Joined
    Aug 2015
    Posts
    4
    Location

    Return Last April using VBA

    Hi,
    I thought this should be relatively easy, but have actually become stuck, I just want a bit of vba code which returns the first day of last april, so in this case with today being 01/02/2016, I would like the date returned to be 01/04/2016.

    Thanks in advance

  2. #2
    VBAX Contributor GarysStudent's Avatar
    Joined
    Aug 2012
    Location
    Lakehurst, NJ, USA
    Posts
    127
    Location
    Consider:
    Public Function FirstOfLastApril() As Date
       Application.Volatile
       Dim d As Date
       d = Date
       If Month(d) = 4 Then
          FirstOfLastApril = DateSerial(Year(d) - 1, 4, 1)
       Else
          For i = 1 To 9999
             d = d - 1
             If Month(d) = 4 And Day(d) = 1 Then
                FirstOfLastApril = d
                Exit Function
             End If
          Next i
       End If
    End Function
    We call this "Brute farce"
    Have a Great Day!

  3. #3
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Function LastAprilFools() As Date
    LastAprilFools = CDate(Format(Now, "1-4-yyyy"))
    End Function
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    so in this case with today being 01/02/2016, I would like the date returned to be 01/04/2016.
    If it were Oct 1, 2016 what would you want? The last April would be April 1, 2016, but the April of last year would be April 1, 2015
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Thanks Paul. You made me realize that my reply wasn't right.
    Function LastAprilFools() As Date
    'If After Apr 1, returns this year's Apr 1 date, else Last year's.
    
    If Date > CDate(Format(Date, "1-4-yyyy")) Then
      LastAprilFools = CDate(Format(Date, "1-4-yyyy"))
    Else
        LastAprilFools = CDate(Format(DateAdd("y", -1, Date), "1-4-yyyy"))
    End If
    
    End Function
    And here is a universal function
    Function AnyAprilFirst(SomeDate As Date) As Date
    'Returns April 1 of year given in SomeDate
      
    AnyAprilFirst = CDate(Format(SomeDate, "1-4-yyyy"))
    End Function
    Last edited by SamT; 02-02-2016 at 10:28 PM.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  6. #6
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Function DateOfQtrOfYear(Qtr As Long, fYear As Date) As Date
    'Returns the start date of the specified Qtr of the year given in fDate
    'Set FiscalBegins according to the date your fiscal year starts in the form Day-Month-
    
    'Note: Fails if Fiscal year begins before 1 Jan.  UnComment next line
    'fYear = DateAdd("y", -1, fYear)
    
    Const FiscalBegins As String = "1-1-"
    DateOfQtrOfYear = DateAdd("q", Qtr - 1, Format(fYear, FiscalBegins & "yyyy"))
    End Function
    Last edited by SamT; 02-02-2016 at 11:05 PM.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  7. #7
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    Sub M_snb()
        MsgBox Format(DateSerial(Year(Date) + (Month(Date) < 4), 4, 1), "dd-mm-yyyy")
    End Sub

Posting Permissions

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