Workbook_BeforeClose event

sanilmathews

Board Regular
Joined
Jun 28, 2011
Messages
102
Hi, I am looking for some help in modifying the below code.
Present Situation: Below code will not allow the user to close the workbook unless Rng1 is filled out.

Requirement: The below code should be modified in a way that if all cells in the range are blank, user can close the workbook. However if "any one" cell in the range is not blank, user should not be allowed to close the workbook unless all cells in the range is filled.

Code:
Private Sub Workbook_BeforeClose (Cancel As Boolean)

Dim Rng1 As Range
Dim Rng2 As Range
Dim Prompt As String
Dim Cell As Range
Dim AllowClose As Boolean

AllowClose = True
Set Rng1 = Sheets ("Sheet1").Range ("B4:F4, B5, C13:C14, C17:C22, C24:C25")
Prompt = "Please ensure to fill all fields"
For Each Cell In Rng1
If Cell.Value = vbNullString Then
AllowClose = False
If Rng2 Is Nothing Then
Set Rng2 = Cell
Else
Set Rng2 = Union(Rng2, Cell)
End If
End If
Next
If AllowClose Then
Else
MsgBox Prompt, vbCritical, "Incomplete Data"
Cancel = True
Rng2. Select
End If
End Sub

Thanks
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Hi
Try following & see if does what you want

Thisworkbook Code page

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Cancel = NotAllowClose(Worksheets("Sheet1"))
End Sub


Standard module

Code:
Function NotAllowClose(ByVal sh As Object) As Boolean
    Dim Rng1 As Range, Cell As Range
    Dim Prompt As String
    
    Prompt = "Please ensure to fill all fields"
    
    Set Rng1 = sh.Range("B4:F4, B5, C13:C14, C17:C22, C24:C25")
    
    With Application
        NotAllowClose = CBool(.CountA(Rng1) > 0 And .CountA(Rng1) <> Rng1.Cells.Count)
    End With
    
    If NotAllowClose Then
    sh.Activate
    For Each Cell In Rng1.Cells
       If Len(Cell.Value) = 0 Then Cell.Select: Exit For
    Next Cell
        MsgBox Prompt, vbCritical, "Incomplete Data"
    End If
End Function

Dave
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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