VBA Save and Clear before exiting

bloodmilksky

Board Regular
Joined
Feb 3, 2016
Messages
202
Hi Guys, I hope everyone is good today :)

I was just wondering if anyone knew the VBA syntax a Sub that would ask the user if they have finished using a message box then if yes clear a range of cells say A1:D4 then save and close the spreadsheet and if no allow them to continue.

any help is as always is greatly appreciated

Many thanks :)
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
What do you mean by "they have finished using a message box" . Maybe you can elaborate a little bit on that
 
Upvote 0
sorry that should have been using a message box ask them if they have finished? if yes clear a range of cells say A1:D4 then save and close the spreadsheet and if no allow them to continue.
 
Upvote 0
Try this:
Code:
Sub Finish_and_Clear()
Dim answer As Integer
answer = MsgBox("Are you through working?", vbYesNo + vbQuestion, "Clear Range")

If answer = vbYes Then
    Range("A1:D4").ClearContents
Else
    Exit Sub
End If

End Sub
 
Upvote 0
Code:
Sub Save_file()
Dim mbox As Integer
mbox = MsgBox("Save file?", vbYesNo, "Save file")
If mbox = vbYes Then
Range("A1:D4").Clear
ActiveWorkbook.SaveAs Filename:="pathfile.extention", FileFormat:=[COLOR="#FF0000"]xlWorkbookNormal[/COLOR]
mbox= MsgBox("File saved", vbOKOnly, "Saved")
Workbooks(1).Close SaveChanges:=False 'change the name of the workbook if necessary
Else
mbox = MsgBox("File not saved", vbOKOnly, "Not saved")
End If
End sub

try this for saving the file
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,077
Messages
6,128,685
Members
449,463
Latest member
Jojomen56

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