ShawnSPS
Board Regular
- Joined
- Mar 15, 2023
- Messages
- 61
- Office Version
- 2003 or older
- Platform
- Windows
Sub SaveNextMonth()
Application.ScreenUpdating = False
Dim mon As String, nextMon As String, fName As String, ws As Worksheet
mon = MonthName(Month(Date))
nextMon = MonthName(Month(Date) + 1)
If MsgBox("The current month will change to " & nextMon & " and all data from the previous month will be deleted. Are you sure you want to change the month and clear all data?", vbYesNo) = vbYes Then
fName = InputBox("Enter the file name to be used.")
If fName = "" Then Exit Sub
ActiveWorkbook.Save
For Each ws In Sheets
If ws.Name <> "Ablank" And ws.Name <> "Zdata" And ws.Name <> "“ZShortCuts”" Then
With ws
.Unprotect ("Pila1DA.#")
.Range("A1") = nextMon
.Range("D3:AH31,D34:AH41").ClearContents
.Protect ("Pila1DA.#")
End With
End If
Next ws
End If
ActiveWorkbook.SaveAs Filename:="C:\Documents and Settings\it0180s\My Documents\" & fName & ".xlsm"
Application.ScreenUpdating = True
End Sub
The current issue I am having with this code is it is delete contents before it being saved so. which I need the data saved first so the user can have the ability to got back in their documents on a month to month basis if they need to check on old reports for a client. The code itself works but I need ActiveWorkbook.SaveAs to save first and then the data cleared. as of right now it clearing out the data before the workbook is saved.
Application.ScreenUpdating = False
Dim mon As String, nextMon As String, fName As String, ws As Worksheet
mon = MonthName(Month(Date))
nextMon = MonthName(Month(Date) + 1)
If MsgBox("The current month will change to " & nextMon & " and all data from the previous month will be deleted. Are you sure you want to change the month and clear all data?", vbYesNo) = vbYes Then
fName = InputBox("Enter the file name to be used.")
If fName = "" Then Exit Sub
ActiveWorkbook.Save
For Each ws In Sheets
If ws.Name <> "Ablank" And ws.Name <> "Zdata" And ws.Name <> "“ZShortCuts”" Then
With ws
.Unprotect ("Pila1DA.#")
.Range("A1") = nextMon
.Range("D3:AH31,D34:AH41").ClearContents
.Protect ("Pila1DA.#")
End With
End If
Next ws
End If
ActiveWorkbook.SaveAs Filename:="C:\Documents and Settings\it0180s\My Documents\" & fName & ".xlsm"
Application.ScreenUpdating = True
End Sub
The current issue I am having with this code is it is delete contents before it being saved so. which I need the data saved first so the user can have the ability to got back in their documents on a month to month basis if they need to check on old reports for a client. The code itself works but I need ActiveWorkbook.SaveAs to save first and then the data cleared. as of right now it clearing out the data before the workbook is saved.