Exit Sub on "Before Close" event

wpryan

Well-known Member
Joined
May 26, 2009
Messages
534
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I have some code that I run on the Before Close event, which does various things, including setting the file name of an output file created by the code. The file name is dependent on certain data entry in one of the worksheets. I need some code that will stop the Close event in case the fields are not complete. What I have is:
VBA Code:
    If Sheets("Instructions").Range("C1") = "" Or Sheets("Instructions").Range("C2") = "" _
    Or Sheets("Instructions").Range("C3") = "" Or Sheets("Instructions").Range("C4") = "" Then
        MsgBox "Please complete required data (Date, Name, LDV Serial Number and Threshold Power", vbOKOnly
Exit Sub
    End If
However, the Exit Sub commands doesn't do what I want it to do, which is stop the entire Close event from happening. What am I missing...?
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Try:
VBA Code:
If Sheets("Instructions").Range("C1") = "" Or Sheets("Instructions").Range("C2") = "" _
    Or Sheets("Instructions").Range("C3") = "" Or Sheets("Instructions").Range("C4") = "" Then
  MsgBox "Please complete required data (Date, Name, LDV Serial Number and Threshold Power", vbOKOnly
  Cancel = True
End If
 
Upvote 0
Solution
Hi
see if this will do what you want

Standard module
VBA Code:
Function IsComplete() As Boolean
    Dim rng As Range
    Set rng = Worksheets("Instructions").Range("C1:C4")
    IsComplete = Application.CountA(rng) = rng.Cells.Count
    If Not IsComplete Then
        MsgBox "Please complete required data (Date, Name, LDV Serial Number and Threshold Power", vbOKOnly
        rng.Parent.Activate
    End If
End Function

Thisworkbook code page
VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Cancel = Not IsComplete
End Sub

Dave
 
Upvote 0
Hi
see if this will do what you want

Standard module
VBA Code:
Function IsComplete() As Boolean
    Dim rng As Range
    Set rng = Worksheets("Instructions").Range("C1:C4")
    IsComplete = Application.CountA(rng) = rng.Cells.Count
    If Not IsComplete Then
        MsgBox "Please complete required data (Date, Name, LDV Serial Number and Threshold Power", vbOKOnly
        rng.Parent.Activate
    End If
End Function

Thisworkbook code page
VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Cancel = Not IsComplete
End Sub

Dave
This looks like a more elegant solution. However, I'm not sure where the Function should be put...
 
Upvote 0
This looks like a more elegant solution. However, I'm not sure where the Function should be put...
I have a need for similar code in another place, however, the cells.count should be in a non-continuous range. Is that possible? I tried:
VBA Code:
Dim wsData As Worksheet: Set wsData = Worksheets("Data")
Dim dRange As Range: Set dRange = wsData.Range("C10:C46, D5, G10:G46, H5, K10:K46, L5")
...but I'm getting an Application-Defined or object defined error...

thanks in advance...
 
Upvote 0
do not get that error

VBA Code:
Dim wsData As Worksheet
Dim dRange As Range

Set wsData = Worksheets("Data")

Set dRange = wsData.Range("C10:C46, D5, G10:G46, H5, K10:K46, L5")

MsgBox dRange.Cells.Count

Dave
 
Upvote 0
do not get that error

VBA Code:
Dim wsData As Worksheet
Dim dRange As Range

Set wsData = Worksheets("Data")

Set dRange = wsData.Range("C10:C46, D5, G10:G46, H5, K10:K46, L5")

MsgBox dRange.Cells.Count

Dave
...strange. after a computer restart I didn't get the error anymore...
 
Upvote 0

Forum statistics

Threads
1,214,627
Messages
6,120,610
Members
448,973
Latest member
ChristineC

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