Hi!
I am using Excel LTSC 2021
I want a code so that user cannot Save as well as Close Excel file until specific range in Sheet is not filled (User cannot Save the file also cannot close the file if any given range is left blank)
Below is the that I have found in other site but the excel file is getting closed without fulfilling the criteria after popping up msgbox. Aslo popping up msgbox even if the criteria is met.
Option Explicit
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("Daily Centre Inputs").Range("B2,G2,B3,F3,I1:I3,C4,D5,D6:D22")
Prompt = "Please check your data ensuring all required " & _
"cells are complete." & vbCrLf & "you will not be able " & _
"to close or save the workbook until the form has been filled " & _
"out completely. " & vbCrLf & vbCrLf & _
"The following cells are incomplete:" & vbCrLf & vbCrLf
For Each Cell In Rng1
If Cell.Value = vbNullString Then
Prompt = Prompt & Cell.Address(False, False) & vbCrLf
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
Any help would be highly appreciated. Apologies for not sharing the file or XL2BB due IT Policy of my organization.
I am using Excel LTSC 2021
I want a code so that user cannot Save as well as Close Excel file until specific range in Sheet is not filled (User cannot Save the file also cannot close the file if any given range is left blank)
Below is the that I have found in other site but the excel file is getting closed without fulfilling the criteria after popping up msgbox. Aslo popping up msgbox even if the criteria is met.
Option Explicit
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("Daily Centre Inputs").Range("B2,G2,B3,F3,I1:I3,C4,D5,D6:D22")
Prompt = "Please check your data ensuring all required " & _
"cells are complete." & vbCrLf & "you will not be able " & _
"to close or save the workbook until the form has been filled " & _
"out completely. " & vbCrLf & vbCrLf & _
"The following cells are incomplete:" & vbCrLf & vbCrLf
For Each Cell In Rng1
If Cell.Value = vbNullString Then
Prompt = Prompt & Cell.Address(False, False) & vbCrLf
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
Any help would be highly appreciated. Apologies for not sharing the file or XL2BB due IT Policy of my organization.