Need help with vba code, I need to have file saved before the data is cleared currently it deletes the data before saving.

ShawnSPS

Board Regular
Joined
Mar 15, 2023
Messages
61
Office Version
  1. 2003 or older
Platform
  1. 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.
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
I think you have to swap the position of the two "Save" commands:
ActiveWorkbook.SaveAs Filename:="C:\Documents and Settings\it0180s\My Documents\" & fName & ".xlsm" just after If fName = "" Then Exit Sub; ActiveWorkbook.Save just before Application.ScreenUpdating = True
 
Upvote 0
I think you have to swap the position of the two "Save" commands:
ActiveWorkbook.SaveAs Filename:="C:\Documents and Settings\it0180s\My Documents\" & fName & ".xlsm" just after If fName = "" Then Exit Sub; ActiveWorkbook.Save just before Application.ScreenUpdating = True
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.SaveAs Filename:="C:\Documents and Settings\it0180s\My Documents\" & fName & ".xlsm"
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.Save
Application.ScreenUpdating = True
End Sub

If ws.Name <> "Ablank" And ws.Name <> "Zdata" And ws.Name <> ""ZShortCuts"" Then -- give me a syntax error after making your suggested changes.
 
Upvote 0
I don't know which process you have in mind; probably you need to save "a copy" of the workbook with the new name and keep the original file before clearing the data...
In this case not ActiveWorkbook.SaveAs but ActiveWorkbook.SaveCopyAs:
VBA Code:
ActiveWorkbook.SaveCopyAs Filename:="C:\Documents and Settings\it0180s\My Documents\" & fName & ".xlsm"

And why ws.Name <> ""ZShortCuts"" and not ws.Name <> "ZShortCuts"?

Please use the vba tag to make your code more readable
 
Upvote 0
Solution

Forum statistics

Threads
1,223,896
Messages
6,175,262
Members
452,627
Latest member
KitkatToby

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