+ Reply to Thread
Results 1 to 7 of 7

Macro Saves & Names File with Current Date & Range

Hybrid View

  1. #1
    Registered User
    Join Date
    01-10-2008
    Posts
    44

    Macro Saves & Names File with Current Date & Range

    Hi, all,

    This is sort of related the post below but with a slight twist.
    http://www.excelforum.com/showthread...highlight=date

    I would like to create a macro that saves and names a file with the current date and the day after. The code below works, but is somewhat limited.

    Sub SaveFileWithDate()
    
    MyMonth = Left(MonthName(Month(Date)), 3)
    MyDay = Day(Date)
    MyDay1 = Day(Date + 1)
    
    MyFileName = "Schedule " & MyMonth & " " & MyDay & " - " & MyMonth & " " & MyDay1 & ".xls"
    
    ActiveWorkbook.SaveAs Filename:="C:\Admin" & MyFileName, FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
            ReadOnlyRecommended:=False, CreateBackup:=False
            
    ActiveWindow.Close
    
    Workbooks.Open "C:\Admin" & MyFileName
       
    End Sub
    What I want is for the macro to only input weekdays. In other words if I run the macro on Fri. 4/11, I want it to name the file "Schedule April 11 - April 14." In this case 4/14 is a Mon. So the macro skips the weekend dates.

    Is there anyway for this to work? Any help would be greatly appreciated.

  2. #2
    Forum Expert
    Join Date
    12-29-2004
    Location
    Michigan, USA
    MS-Off Ver
    2013
    Posts
    2,208
    Will you always only be creating this file Mon-Fri? If so, you can define your variables as follows:
    MyMonth = Format(Date, "mmm")
    MyDay = Day(Date)
    If Weekday(Date + 1, vbMonday) > 5 Then
        MyDay1 = Day(Date + 3)
    Else
        MyDay1 = Day(Date + 1)
    End If
    HTH

    Jason

  3. #3
    Registered User
    Join Date
    01-10-2008
    Posts
    44
    Yes. This particular report will only be created with a Mon-Fri date.

    I tried the code you provided, and it totally worked. It was exactly what I needed. I was even able to use it as a template for another macro I was using. It fixed a similar problem there, too.

    Thanks a lot!

  4. #4
    Forum Expert
    Join Date
    12-29-2004
    Location
    Michigan, USA
    MS-Off Ver
    2013
    Posts
    2,208
    Glad it helped.

    Jason

  5. #5
    Registered User
    Join Date
    01-10-2008
    Posts
    44
    Hi,

    It's me again. So the code that jasoncw provided continues to work great, but I have noticed when it comes to the end of the month, the file is renamed with the same month.

    So if I were to save it today 5/30 the file would be saved as "Schedule May 30 - May 2.xls" How can I get it to say "Schedule May 30 - June 2.xls" Basically at the end of the month, I would like it to get renamed with the following month included.

    This is what I have so far.


    Sub SaveFileWithDate()
    
    MyMonth = Format(Date, "mmm")
    MyDay = Day(Date)
    
    If Weekday(Date + 1, vbMonday) > 5 Then
        MyDay1 = Day(Date + 3)
    Else
        MyDay1 = Day(Date + 1)
    End If
    
    
    MyFileName = "Schedule " & MyMonth & " " & MyDay & " - " & MyMonth & " " & MyDay1 & ".xls"
    
    ActiveWorkbook.SaveAs Filename:="C:\Admin" & MyFileName, FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
            ReadOnlyRecommended:=False, CreateBackup:=False
            
    ActiveWindow.Close
    
    Workbooks.Open "C:\Admin" & MyFileName
       
    End Sub

    Thanks!

  6. #6
    Forum Expert
    Join Date
    12-29-2004
    Location
    Michigan, USA
    MS-Off Ver
    2013
    Posts
    2,208
    See the added IF statement and new variable (MyMonth1) in red below that should correct this issue:

    Sub SaveFileWithDate()
    
    MyMonth = Format(Date, "mmm")
    MyDay = Day(Date)
    
    If Weekday(Date + 1, vbMonday) > 5 Then
        MyMonth1 = Format(Date + 3, "mmm")
    Else
        MyMonth1 = Format(Date + 1, "mmm")
    End If
    
    If Weekday(Date + 1, vbMonday) > 5 Then
        MyDay1 = Day(Date + 3)
    Else
        MyDay1 = Day(Date + 1)
    End If
    
    
    MyFileName = "Schedule " & MyMonth & " " & MyDay & " - " & MyMonth1 & " " & MyDay1 & ".xls"
    
    ActiveWorkbook.SaveAs Filename:="C:\Admin" & MyFileName, FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
            ReadOnlyRecommended:=False, CreateBackup:=False
            
    ActiveWindow.Close
    
    Workbooks.Open "C:\Admin" & MyFileName
       
    End Sub
    HTH

    Jason

+ 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