+ Reply to Thread
Results 1 to 7 of 7

Thread: Add Previous Month to Filename

  1. #1
    Forum Contributor
    Join Date
    08-17-2009
    Location
    West Midlands
    MS-Off Ver
    Excel 2007
    Posts
    121

    Add Previous Month to Filename

    Afternoon all -

    I've written some vba code to save the workbook with the current month:

    ActiveWorkbook.SaveAs Filename:= _
    "P:\filename" & Format(Date, " mmm") & ".xls"

    which works fine - brings back filename MAY

    How can I tweak this code to make it bring back the previous month, i.e. APR?

    Any help much appreciated.

    Cheers

    Steve
    Last edited by SAsplin; 05-17-2011 at 08:59 AM.

  2. #2
    Forum Guru Domski's Avatar
    Join Date
    12-14-2009
    MS-Off Ver
    What does it matter?
    Posts
    3,933

    Re: Add Previous Month to Filename

    Hi,

    Try:

    ActiveWorkbook.SaveAs Filename:= _
    "P:\filename" & Format(DateSerial(Year(Date), Month(Date), 0), " mmm") & ".xls"

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  3. #3
    Forum Contributor
    Join Date
    08-17-2009
    Location
    West Midlands
    MS-Off Ver
    Excel 2007
    Posts
    121

    Re: Add Previous Month to Filename

    Hi Dom

    Thanks for the post. It now highlights DateSerial and comes back with a 'compile error':

    Argument not optional.

    Steve

  4. #4
    Forum Guru Domski's Avatar
    Join Date
    12-14-2009
    MS-Off Ver
    What does it matter?
    Posts
    3,933

    Re: Add Previous Month to Filename

    Works fine for me. Have you copied the code I posted exactly?

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  5. #5
    Forum Contributor
    Join Date
    08-17-2009
    Location
    West Midlands
    MS-Off Ver
    Excel 2007
    Posts
    121

    Re: Add Previous Month to Filename

    Thanks Marco,

    Works great. User error as always!!

    Steve

  6. #6
    Forum Guru Domski's Avatar
    Join Date
    12-14-2009
    MS-Off Ver
    What does it matter?
    Posts
    3,933

    Re: Add Previous Month to Filename

    Marco's not here
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  7. #7
    Forum Contributor
    Join Date
    08-17-2009
    Location
    West Midlands
    MS-Off Ver
    Excel 2007
    Posts
    121

    Re: Add Previous Month to Filename

    Brilliant. I can't even remember names! Thanks Dom!!

+ 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.2.0