+ Reply to Thread
Results 1 to 5 of 5

macro to change date

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-06-2013
    Location
    toronto
    MS-Off Ver
    Excel 2010
    Posts
    103

    macro to change date

    hello,

    a macro that changes the date in , say, A1, but i want the date to be the PREVIOUS business day. so today is april 12, i press the macro so it shows april 11. Or say on Monday, April 15, when I press the button the date shows Friday, April 12.

    Thanks!

  2. #2
    Valued Forum Contributor Sean Thomas's Avatar
    Join Date
    03-25-2012
    Location
    HerneBay, Kent, UK
    MS-Off Ver
    Excel 2007,2016
    Posts
    971

    Re: macro to change date

    Hi, try this

    you might want to put some extra code in to make sure selection is no more than 1 cell

    Sub Weekday()
    Dim TDate, YDate As Date
    Dim NDate As Integer
    TDate = Selection
    NDate = WorksheetFunction.Weekday(TDate, 2) '("12/04/13")
    Select Case NDate
        Case 7
        YDate = TDate - 2
        Case 2, 3, 4, 5, 6
        YDate = TDate - 1
        Case 1
        YDate = TDate - 3
    End Select
    Selection = YDate
    End Sub
    Regards
    Sean

    Please add to my reputation if you think i helped
    (click on the star below the post)
    Mark threads as "Solved" if you have your answer
    (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code:
    [code] Your code here [code]
    Please supply a workbook containing example Data:
    It makes its easier to answer your problem & saves time!

  3. #3
    Forum Contributor
    Join Date
    02-06-2013
    Location
    toronto
    MS-Off Ver
    Excel 2010
    Posts
    103

    Re: macro to change date

    I get "run-time error '1004': application-defined or objected-defined error"

    when i debug, "selection = ydate" is highlighted

  4. #4
    Forum Contributor
    Join Date
    02-06-2013
    Location
    toronto
    MS-Off Ver
    Excel 2010
    Posts
    103

    Re: macro to change date

    I should also mention that it will adding one business day...but will be one business day before today, lol

    so, for example, I open the file today...it shows "April 10"...and I want the date to change to April 11 (today is April 12, so one business day before)

    format....MM/DD/YYYY

  5. #5
    Valued Forum Contributor Sean Thomas's Avatar
    Join Date
    03-25-2012
    Location
    HerneBay, Kent, UK
    MS-Off Ver
    Excel 2007,2016
    Posts
    971

    Re: macro to change date

    This works good for what you originally asked for

    Sub Weekday()
    Dim TDate, YDate As Date
    Dim NDate As Integer
    If Selection.Cells.Count > 1 Or IsEmpty(Selection) Then Exit Sub
    TDate = Selection
    NDate = WorksheetFunction.Weekday(TDate, 2) '("12/04/13")
    Select Case NDate
        Case 7
        YDate = TDate - 2
        Case 2, 3, 4, 5, 6
        YDate = TDate - 1
        Case 1
        YDate = TDate - 3
    End Select
    Selection = Format(YDate, "MM/DD/YYYY")
    End Sub
    Make sure you have selected a cell before running macro.

    the only way you can get the cell to contain todays date -1 when is to run another macro.
    I would suggest placing one in this workbook module for workbook_open procedure

    something like Sheet1.Range("A1"). value = today()

    you cant use the worksheet function because the other macro will always overwrite it.

+ 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