Macro to stop me closing a workbook if F1 is empty?

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
3,201
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi Everyone,

I need a macro that when I go to close the workbook down it looks at sheet "welcome" cell F1 and if it says "Submitted" then closes but if not brings up a message box saying
"Data not sent"
"Do You wish to close document?"
"Any Changes will be lost"

Yes No,

if they click yes it closes
if they click no it stays open.

Please help if you can

Thanks

Tony
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
How about:

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next
If Sheets("Welcome").Range("F1") <> "Submitted" Then
promptit = MsgBox("Changes will be lost. Do you want to close document?", vbYesNo, "DATA NOT SENT")
If promptit = vbNo Then
    Cancel = True
    Exit Sub
End If
End If
Resume Next
End Sub
 
Upvote 0
This code should go into the thisworkbook module. If the user clicks yes then the file closes and does not popup to save changes. There is nothing to stop the user from saving before closing. If F1=submitted then the file closes as normal and will save the file before closing.

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If Not UCase(Sheets("welcome").Range("F1")) = "SUBMITTED" Then
    ans = MsgBox("Data not sent" & Chr(10) & "Do You wish to close document?" & Chr(10) & "Any Changes will be lost", vbYesNo)
    ThisWorkbook.Saved = True
Else
ThisWorkbook.Save
End If
If ans = vbNo Then Cancel = True
End Sub
 
Upvote 0
Thank you Roderick and Scott, both a great help, both do a great job and exactly what I wanted.

Thank you for your help

Tony
 
Upvote 0

Forum statistics

Threads
1,216,503
Messages
6,131,020
Members
449,615
Latest member
Nic0la

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