+ Reply to Thread
Results 1 to 4 of 4

Excel Macro: Ask user for date then subtract 6 months from it

Hybrid View

  1. #1
    Registered User
    Join Date
    10-24-2012
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2013
    Posts
    87

    Excel Macro: Ask user for date then subtract 6 months from it

    Hello,

    I am trying to create a macro that will ask a user to enter a date (format is: mm/dd/yyyy)

    I then want to create a variable that will deduct 6 months from the date entered. I think I am close but not quite there. I can get the 6 months from the CURRENT date, but not the date entered by the user.

    Any help would be greatly appreciated


    Dim DateAsk As Date
    Dim DateWithin6Months As Date
    
    
    DateAsk = InputBox("Enter the Date the document was extracted in (MM-DD-YYYY) Format")
    
    
    
        DateWithin6Months = DateAdd("m", -6, Date)
        DateWithin6Months = Format(DateWithin6Months, "mm/dd/yyyy")
    Last edited by foxy_420; 03-21-2013 at 11:09 AM.

  2. #2
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Excel Macro: Ask user for date then subtract 6 months from it

    Try:

    Sub Date_Macro()
    Dim DateAsk As String
    Dim DateWithin6Months As Date
    
    
    DateAsk = InputBox("Enter the Date the document was extracted in (MM-DD-YYYY) Format")
    
    If Not IsDate(DateAsk) Then
        DateAsk = InputBox("Re-Enter: Must be in date format (MM-DD-YYYY)")
    End If
    
        DateWithin6Months = DateAdd("m", -6, DateAsk)
        DateWithin6Months = Format(DateWithin6Months, "mm/dd/yyyy")
    
    MsgBox DateWithin6Months 'this can be removed.  Here for testing purposes
    
    End Sub
    - Changed DateAsk to string to allow for error check
    - added in error check
    - edited DateWithin6Months to reference DateAsk not Date

  3. #3
    Registered User
    Join Date
    08-22-2012
    Location
    Zagreb, Croatia
    MS-Off Ver
    Excel 2003
    Posts
    61

    Re: Excel Macro: Ask user for date then subtract 6 months from it

    Hi,
    possibly have to try with DateAsk instead of Date in this sentence:
    DateWithin6Months = DateAdd("m", -6, DateAsk)

    Regards,
    If a post helps press star sign 4 my reputation

  4. #4
    Registered User
    Join Date
    10-24-2012
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2013
    Posts
    87

    Re: Excel Macro: Ask user for date then subtract 6 months from it

    Thanks guys, this works perfect. Mnjofra, your answer was very simple to fix and worked great. I also do very much appreciate your reply to stnkynts. I'll definitely add to both your feedback

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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