+ Reply to Thread
Results 1 to 14 of 14

Macro for last business date

Hybrid View

  1. #1
    Registered User
    Join Date
    01-30-2014
    Location
    Bangalore
    MS-Off Ver
    Excel 2010
    Posts
    20

    Post Macro for last business date

    Hi All,

    Can you please help me to create a macro?

    I want to add the Last business date in B3 cell just by running a macro.

    Thanks in advance
    Nikhil

  2. #2
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Macro for last business date

    What is the definition of 'Last Business Date' ?
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  3. #3
    Registered User
    Join Date
    01-30-2014
    Location
    Bangalore
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Macro for last business date

    Hi Olly,

    I mean, for Monday, that last business day was Friday & for Friday its Thursday.

    Thanks

  4. #4
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Macro for last business date

    Quote Originally Posted by Nikhil V V View Post
    I mean, for Monday, that last business day was Friday & for Friday its Thursday.
    Ok. Try:
    Sub foo()
    Select Case Weekday(Date, vbMonday)
        Case 1:  Range("B3").Value = Date - 3
        Case Else:  Range("B3").Value = Date - 1
    End Select
    End Sub

  5. #5
    Registered User
    Join Date
    01-30-2014
    Location
    Bangalore
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Macro for last business date

    This is Great!!Thanks Olly, Below is the code which i have made with the help of recording tool as well the date macro you gave. However this gives me an error as "Cant execute the code in break mode"

    Sub Macro3()
    '
    ' Macro3 Macro
    'Range("B4").Select
    Sheets("Unwind & Reset").Select
    Rows("1:1").Select
    ActiveSheet.ShowAllData
    Sheets("Cash Activity Report").Select
    Selection.ClearContents
    Sheets("DATA").Select
    Cells.Select
    Selection.ClearContents
    Range("E26").Select
    Sheets("Cash Activity Report").Select
    Range("A1").Select
    Select Case Weekday(Date, vbMonday)
    Case 1: Range("B3").Value = Date - 3
    Case Else: Range("B3").Value = Date - 1
    End Select


    End Sub


    I am gettiing the error when it reaches Here " ActiveSheet.ShowAllData"

    Please help me out to fix this

    Thanks

  6. #6
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Macro for last business date

    1. Wrap CODE tags around your code when you post.
    2. To exit break mode, click the 'reset' button (blue square) on the toolbar in the visual basic editor.
    3. There's a lot of unnecessary selecting going on there... Are you trying to clear the contents of ALL cells in worksheets [Cash Activity Report] and [DATA], or only certain ranges (and if only certain ranges, what are those range addresses?)

    Here's a rewrite of your code without all that unnecessary selecting - but beware, this will clear the contents of ALL cells in those two worksheets... If this isn't what you want, you need to specify the actual range to be cleared.

    Sub Macro3()
    Sheets("Unwind & Reset").ShowAllData
    Sheets("DATA").Cells.ClearContents
    With Sheets("Cash Activity Report")
        .Cells.ClearContents
        Select Case Weekday(Date, vbMonday)
            Case 1: Range("B3").Value = Date - 3
            Case Else: Range("B3").Value = Date - 1
        End Select
    End With
    End Sub

  7. #7
    Registered User
    Join Date
    01-30-2014
    Location
    Bangalore
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Macro for last business date

    Hi Olly,

    I want this date format as mm-dd-yyyy for this;

    Sub foo()
    Select Case Weekday(Date, vbMonday)
    Case 1: Range("B3").Value = Date - 3
    Case Else: Range("B3").Value = Date - 1
    End Select
    End Sub


    Can you please help me with this. At present this is giving me dd-mm-yyyy
    Last edited by Nikhil V V; 01-30-2014 at 06:07 PM.

  8. #8
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Macro for last business date

    Change the numberformat of cell B3.

    And again, wrap CODE tags around code you post...

  9. #9
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Macro for last business date


  10. #10
    Forum Contributor
    Join Date
    12-14-2013
    Location
    Tilburg, Nederland
    MS-Off Ver
    Excel 2010
    Posts
    256

    Re: Macro for last business date

    Sub DateText()
        Range("B3").Value = Format(Date, "dd mmm YYYY")
    End Sub

  11. #11
    Registered User
    Join Date
    01-30-2014
    Location
    Bangalore
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Macro for last business date

    Hi Gerard,

    Thanks. But this gives me the current date. I want it like this: for Monday, Last business date is Friday. Likewise for Friday it is Thursday.

    Please help me with this.

    Thanks

+ 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. Replies: 0
    Last Post: 03-21-2013, 12:22 PM
  2. macro for changing the color or a row depending on the date - business days
    By leung.kevin8 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-21-2010, 03:07 PM
  3. Calculating business day from date
    By svs9j in forum Excel General
    Replies: 6
    Last Post: 09-14-2009, 06:31 AM
  4. Due date is 6 business days
    By Wzbell in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 05-30-2007, 07:55 PM
  5. [SOLVED] Macro to highlight cells that are 5 business days from trade date
    By Jason via OfficeKB.com in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-09-2005, 04:06 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