Beforeclose event - help

newbie1987

New Member
Joined
Jun 29, 2023
Messages
4
Office Version
  1. 365
Hi, new here. was wondering if I could get some help on basic coding.

I am trying to create a before close event to stop someone exiting workbook before filling in certain cells. I am able to put code across 1 field but not multiple.

Code below:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
If Cells(10, 5) = "" Then
MsgBox "this field is required", vbInformation, "REQUIRED"
Cancel = True
End If
End Sub


However i need this on (10,7) and (10,8) and (10,14) (10,15) (10,16) with different messages for each.

thanks,
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Welcome to the Board!

One simple way is to just repeat the IF...THEN block under the existing one for each one, i.e.
repeat this part:
Rich (BB code):
If Cells(10, 5) = "" Then
MsgBox "this field is required", vbInformation, "REQUIRED"
Cancel = True
End If
changing the address and message for each one.
 
Upvote 0
Solution
thanks for coming back to me, however I am still having an issue. see below:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
If Cells(10, 5) = "" Then
MsgBox "req1", vbInformation, "REQUIRED"
If Cells(7, 5) = "" Then
MsgBox "Req2", vbInformation, "REQUIRED"
If Cells(8, 5) = "" Then
MsgBox "Req3", vbInformation, "REQUIRED"
If Cells(14, 5) = "" Then
MsgBox "Req4", vbInformation, "REQUIRED"
If Cells(15, 5) = "" Then
MsgBox "Req5", vbInformation, "REQUIRED"
If Cells(16, 5) = "" Then
MsgBox "Req6", vbInformation, "REQUIRED"
Cancel = True
End If
End Sub
 
Upvote 0
You are welcome.
Glad I was able to help.
 
Upvote 1
There is still a workaround that a person can SAVEAS , is there a way to stop them saving the form until they have actioned the above.

above only notifies them if they try and exit form via X in corner but they can get round it by saving as.

thanks
 
Upvote 0
There is still a workaround that a person can SAVEAS , is there a way to stop them saving the form until they have actioned the above.

above only notifies them if they try and exit form via X in corner but they can get round it by saving as.

thanks
There is also a "BeforeSave" event that you can add code to.
 
Upvote 1

Forum statistics

Threads
1,224,707
Messages
6,180,460
Members
452,986
Latest member
zenki1

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