VBA Save as on desktop with previous month's first and last dates

cdalgorta

Board Regular
Joined
Jun 5, 2022
Messages
87
Office Version
  1. 365
Platform
  1. Windows
Hi,
Still beginner here.
I've been trying to find the way with Google, but there are too many different response and none have worked for me so far.
I need to save a file with this name(based on today being August 2022) to my desktop:
"2022-07-01 to 2022-07-31 Consolidated file.xlsx"

"The first date for last month" "to" "the last date of last month" "Consolidated file"​


I've tried this last(which I'm sure is totally wrong hahah), but I still get error on the last line.
Please help if you can.
Thank you in advance!


Sub testingSaveAs()

Dim wb As Workbook
Dim mydate As Date
mydate = Now()

Filename = ActiveWorkbook.Name
user = Environ("Username")
desktop = "C:\Users\" & user & "\Desktop\"
FirstDay = Format(DateSerial(Year(mydate), Month(mydate), 1), "YYYY-MM-DD")
LastDay = Format(DateSerial(Year(mydate), Month(mydate), 1) - 1, "YYYY-MM-DD")



wb.SaveAs Filename:=desktop & FirstDay & " to " & LastDay & " Consolidated File Peloton.xlsx"

End Sub


I thought it would be easy and all I'd need to do is use the eomonth function for the format:
FirstDay = Format(EOMONTH(NOW(),-2)+1, "YYYY-MM-DD")
but it gave me error on the Eomonth for some reason. 😞
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Is this what you are trying?

VBA Code:
Option Explicit

Sub Sample()
    Dim wb                      As Workbook
    Dim FlName                  As String
    Dim FilePath                As String
    Dim FirstDayOfLastMonth     As String
    Dim LastDayOfLastMonth      As String
    
    Set wb = ThisWorkbook
    
    FilePath = "C:\Users\" & Environ("Username") & "\Desktop\"
    
    FirstDayOfLastMonth = Format(DateSerial(Year(Date), Month(Date) - 1, 1), "YYYY-MM-DD")
    LastDayOfLastMonth = Format(DateSerial(Year(Date), Month(Date), 0), "YYYY-MM-DD")

    FlName = FilePath & FirstDayOfLastMonth & " to " & LastDayOfLastMonth & " Consolidated File Peloton.xlsx"

    Debug.Print FlName
    'OUTPUT: C:\Users\Username\Desktop\2022-07-01 to 2022-07-31 Consolidated File Peloton.xlsx
    
    'wb.SaveAs Filename:=FlName, FileFormat:=51
End Sub

Few other pointers:
  1. Avoid using too many variables.
  2. Always declare your variables.
  3. While using .SaveAs, do not skip FileFormat.
 
Upvote 0
Solution
Hi,
Still beginner here.
I've been trying to find the way with Google, but there are too many different response and none have worked for me so far.
I need to save a file with this name(based on today being August 2022) to my desktop:
"2022-07-01 to 2022-07-31 Consolidated file.xlsx"

"The first date for last month" "to" "the last date of last month" "Consolidated file"​


I've tried this last(which I'm sure is totally wrong hahah), but I still get error on the last line.
Please help if you can.
Thank you in advance!


Sub testingSaveAs()

Dim wb As Workbook
Dim mydate As Date
mydate = Now()

Filename = ActiveWorkbook.Name
user = Environ("Username")
desktop = "C:\Users\" & user & "\Desktop\"
FirstDay = Format(DateSerial(Year(mydate), Month(mydate), 1), "YYYY-MM-DD")
LastDay = Format(DateSerial(Year(mydate), Month(mydate), 1) - 1, "YYYY-MM-DD")



wb.SaveAs Filename:=desktop & FirstDay & " to " & LastDay & " Consolidated File Peloton.xlsx"

End Sub


I thought it would be easy and all I'd need to do is use the eomonth function for the format:
FirstDay = Format(EOMONTH(NOW(),-2)+1, "YYYY-MM-DD")
but it gave me error on the Eomonth for some reason. 😞

Is this what you are trying?

VBA Code:
Option Explicit

Sub Sample()
    Dim wb                      As Workbook
    Dim FlName                  As String
    Dim FilePath                As String
    Dim FirstDayOfLastMonth     As String
    Dim LastDayOfLastMonth      As String
   
    Set wb = ThisWorkbook
   
    FilePath = "C:\Users\" & Environ("Username") & "\Desktop\"
   
    FirstDayOfLastMonth = Format(DateSerial(Year(Date), Month(Date) - 1, 1), "YYYY-MM-DD")
    LastDayOfLastMonth = Format(DateSerial(Year(Date), Month(Date), 0), "YYYY-MM-DD")

    FlName = FilePath & FirstDayOfLastMonth & " to " & LastDayOfLastMonth & " Consolidated File Peloton.xlsx"

    Debug.Print FlName
    'OUTPUT: C:\Users\Username\Desktop\2022-07-01 to 2022-07-31 Consolidated File Peloton.xlsx
   
    'wb.SaveAs Filename:=FlName, FileFormat:=51
End Sub

Few other pointers:
  1. Avoid using too many variables.
  2. Always declare your variables.
  3. While using .SaveAs, do not skip FileFormat.
Thank you so much Siddharth!
I'll follow your pointers from now on 😊
 
Upvote 0

Forum statistics

Threads
1,223,902
Messages
6,175,278
Members
452,629
Latest member
SahilPolekar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top