+ Reply to Thread
Results 1 to 6 of 6

Filename as PRIOR month

Hybrid View

  1. #1
    Registered User
    Join Date
    11-30-2007
    Location
    Edinburgh, UK
    MS-Off Ver
    2003
    Posts
    84

    Filename as PRIOR month

    Hi,

    I'm trying to get a bit of code to name a file as the prior month. I searching this forum and many others, as well as excel help, and even asked our traning department in work, all to no avail.

    Does anyone know how I can get this to work?

    This is what I have:

    
    Sub save()
    
    Dim myPath As String, lastday As String
        myPath = ActiveWorkbook.Path
    'lastday need to be the end of the prior month in yyyymmdd format, so far only saves as current date in yyyymmdd format (need to work for year end as well)
        lastday = Format(Now(), "yyyy") & Format(Now(), "mm") & Format(Now(), "dd")
    'which could be lastday = Format(Now(), "yyyymmdd") I know, but I was working on trying to get it to work.
          
        ActiveWorkbook.SaveAs Filename:= _
        myPath & "\Final_RP_Barra_Preprocessor_TEST " & lastday & ".xls"
    
    End Sub
    Cheers
    Last edited by VBA Noob; 05-23-2008 at 11:00 AM.

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481
    Try

    
    lastday  = format(dateserial(year(date),month(date),0),"yyyymmdd")
    Cheers
    Andy
    www.andypope.info

  3. #3
    Registered User
    Join Date
    11-30-2007
    Location
    Edinburgh, UK
    MS-Off Ver
    2003
    Posts
    84
    genius!

    Thank you very much

    edit - I was probably looking in the wrong place, but couldn't find it anywhere, and no one I asked in work knew!

    That's gonna make things a lot easier - thanks!

  4. #4
    Registered User
    Join Date
    11-30-2007
    Location
    Edinburgh, UK
    MS-Off Ver
    2003
    Posts
    84
    Is there any chance you could break that down for me please?

    I'd like to know how it works if I can.

    what part of the code does what?

  5. #5
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481
    Well DateSerial returns a date based on Year, Month and Day values.

    The trick is a day of Zero will cause the function to return the day before the 1st of the month, which is the last day of the previous month.

  6. #6
    Registered User
    Join Date
    11-30-2007
    Location
    Edinburgh, UK
    MS-Off Ver
    2003
    Posts
    84
    cool, thanks very much. That's a good price of code to know

+ 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