Overwrite msgbox Yes/No issue

Jonseyt23

New Member
Joined
Nov 9, 2017
Messages
5
Hello

I am in need of your help, I am trying to add a message box popup in VBA that when a user selects file>save or ctrl+s, it prompts them with a message box that states "Do you really want to overwrite this file?" Yes button to continue to save, No button to cancel and close the message box.

I tried using the below code but pressing no still saves it.
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
MsgBox "Do you really want to overwrite this file?", vbYesNo
End Sub

I am not very clued up on VBA so I might be missing something simple, but cant figure it out.

Best Regards

Dave
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Hi, welcome to the forum!

You need to test what the result of the msgbox is and cancel the save if required - for example:

Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
  If MsgBox("Do you really want to overwrite this file?", vbYesNo) = vbNo Then Cancel
End Sub
 
Last edited:
Upvote 0
Hello, thank you for you're reply, the result of the current msgbox string is, box come up, pressing Yes saves it, pressing No also saves it.

I have tried adding the
vbYesNo) = vbNo Then Cancel, like in the example. but now getting a Compile error - Expected sub, Function or Property within the VBA editor
 
Upvote 0
now getting a Compile error

Sorry, it should be..

Rich (BB code):
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
  If MsgBox("Do you really want to overwrite this file?", vbYesNo) = vbNo Then Cancel = True
End Sub
 
Upvote 0
Just a little addition to this msg box if it can be done, how can I add the vbExclamation to give it a bit more of a visual appearance.
 
Upvote 0
Hi, you can try like this.:

Rich (BB code):
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
  If MsgBox("Do you really want to overwrite this file?", vbYesNo + vbExclamation) = vbNo Then Cancel = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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