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.
Thanks
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