PDA

View Full Version : [SOLVED:] Exit excel if the cells are clean



osevero
11-14-2013, 03:54 AM
Hi guys!

I need to know a code where it's only possible to exit excel if cell C5 and D7 of spreadsheet "Sheet1" is empty, and the cell J2 of spreadsheet "Sheet2" is empty. If not empty, a message: "To exit the program you have to clean."

Help please

Cheers

P.S: Are also trying to help me on this forum: http://www.mrexcel.com/forum/excel-questions/739299-exit-excel-if-cells-clean.html#post3633791

SamT
11-14-2013, 06:33 AM
To find help on this subject:


It is about the Workbook, so type "Workbook" in VBA and leave the cursor in or next to the word, then press F1
It is about the Close process, so it is an Event. Click on the "Event" menu and look for anything about "Close" and you will find "BeforeCLose."


ThisWorkbook Code Page

Option Explicit 'Only goes at top of page

Private Sub Workbook_BeforeClose(Cancel As Boolean)
With Sheets("Sheet1")
If Range("C5") <> "" Or Range("D7") <> "" Then
Cancel = True 'Cancels the Closing process
MsgBox "Message here"
Exit Sub
End If
End With

If Sheets("Sheet2").Range("J2") <> "" Then
Cancel = True
MsgBox "Message here"
Exit Sub
End If
End Sub

osevero
11-14-2013, 11:36 AM
Awesome! It's working :bow: Thanks SamT