Sub OpenAndSaveAsNextMonth()
' Define variables
Dim currentMonth As String
Dim nextMonth As String
Dim filePath As String
Dim fileName As String
Dim currentWorkbook As Workbook
Dim nextWorkbook As Workbook
' Define file path and file name
filePath = "X:\Plant-Flora\Assembly-Flora\Public\Mike Prosser\Manning Actual\"
fileName = "March 2023 KPI manning Rev 2d.xlsm"
' Get current month and next month
currentMonth = Format(Date, "mmmm yyyy")
nextMonth = Format(DateAdd("m", 1, Date), "mmmm yyyy")
' Check if it is the 3rd day of the current month
If Day(Date) = 3 Then
' Open the current month file
Set currentWorkbook = Workbooks.Open(filePath & fileName)
' Save as the next month
currentWorkbook.SaveAs filePath & Replace(fileName, currentMonth, nextMonth), 52
' Update cell D2 on sheet 2 to the next month
currentWorkbook.Sheets("2").Range("D2").Value = DateSerial(Year(currentWorkbook.Sheets("2").Range("D2").Value), Month(currentWorkbook.Sheets("2").Range("D2").Value) + 1, 1)
' Update cell D2 on summary sheet to represent month in number form
currentWorkbook.Sheets("Summary").Range("D2").Value = Month(currentWorkbook.Sheets("2").Range("D2").Value)
' Close the next month file and save changes
Application.DisplayAlerts = False ' Suppress save prompt
currentWorkbook.Close SaveChanges:=True
Application.DisplayAlerts = True ' Reset save prompt
End If
End Sub
The bold part of the code is causing an error. I am trying to leave my March file open after the April file has been created, updated, saved and closed. However, it will either leave the April file open, both files open or close both and leave a blank excel open. Please help correct.
' Define variables
Dim currentMonth As String
Dim nextMonth As String
Dim filePath As String
Dim fileName As String
Dim currentWorkbook As Workbook
Dim nextWorkbook As Workbook
' Define file path and file name
filePath = "X:\Plant-Flora\Assembly-Flora\Public\Mike Prosser\Manning Actual\"
fileName = "March 2023 KPI manning Rev 2d.xlsm"
' Get current month and next month
currentMonth = Format(Date, "mmmm yyyy")
nextMonth = Format(DateAdd("m", 1, Date), "mmmm yyyy")
' Check if it is the 3rd day of the current month
If Day(Date) = 3 Then
' Open the current month file
Set currentWorkbook = Workbooks.Open(filePath & fileName)
' Save as the next month
currentWorkbook.SaveAs filePath & Replace(fileName, currentMonth, nextMonth), 52
' Update cell D2 on sheet 2 to the next month
currentWorkbook.Sheets("2").Range("D2").Value = DateSerial(Year(currentWorkbook.Sheets("2").Range("D2").Value), Month(currentWorkbook.Sheets("2").Range("D2").Value) + 1, 1)
' Update cell D2 on summary sheet to represent month in number form
currentWorkbook.Sheets("Summary").Range("D2").Value = Month(currentWorkbook.Sheets("2").Range("D2").Value)
' Close the next month file and save changes
Application.DisplayAlerts = False ' Suppress save prompt
currentWorkbook.Close SaveChanges:=True
Application.DisplayAlerts = True ' Reset save prompt
End If
End Sub
The bold part of the code is causing an error. I am trying to leave my March file open after the April file has been created, updated, saved and closed. However, it will either leave the April file open, both files open or close both and leave a blank excel open. Please help correct.