Frncis
Member
I have VBA code that prevents closing the workbook/application with the red X, or going to another work sheet if a cell range contains RED or Yellow. The code does work as expected. However, there is one problem, that I did not foresee. It also prevents you from closing the application. The code is based on Cell range F4: F17 containing "No". I am wondering if it is possible to modify the code, so the user can't move on to another worksheet, but can exit, and leave the color filled cells, filled? This cell range for closure would be Cell range F4: F17 containing "No".
I have included both the code to fill the cells & to prevent the closure due to filled cells. So you have an idea of the entire code.
Here is the code to fill the cells:
Here is the code to prevent closure & going to another worksheet:
This part of the code is located in This workbook:
Part 2 of the code is located in module 1:
I have Googeled to as of now, I have not found anything close to solving this problem.
I have included both the code to fill the cells & to prevent the closure due to filled cells. So you have an idea of the entire code.
Here is the code to fill the cells:
Code:
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
'This code fills a certain cell range with either Red or Yellow.
Dim rngToTest As Range
Dim rngCel As Range
Dim lngColor As Long
On Error GoTo ReEnableEvents
Application.EnableEvents = False
lngColor = RGB(255, 0, 0) 'Edit the RGB code if required
lngColor = RGB(255, 255, 0) 'Edit the RGB code if required
'Test if the string "Meeting" appears in the worksheet name.
If InStr(1, Sh.Name, "Meeting", vbTextCompare) > 0 Then
With Sh
Set rngToTest = .Range("B4:P17") 'Edit the range if required.
For Each rngCel In rngToTest
If rngCel.DisplayFormat.Interior.Color = lngColor Then
Application.Goto rngCel
MsgBox " Cannot exit worksheet while any cells are highlighted in Red or Yellow" & vbCrLf _
& "Please complete appropriate cells.", vbExclamation, Title:="Voc. Rehab - Career Link Data Entry"
GoTo ReEnableEvents
End If
Next rngCel
End With
End If
ReEnableEvents:
If Err.Number <> 0 Then
MsgBox "An error occurred in ThisWorkbook module, Private Sub Workbook_SheetDeactivate"
End If
Application.EnableEvents = True
End Sub
Here is the code to prevent closure & going to another worksheet:
This part of the code is located in This workbook:
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
'Part of code to call a pop up calendar.
On Error Resume Next
Application.OnKey "+^{C}"
Application.CommandBars("Cell").Controls("Insert Date").Delete
'Prevents closure by clicking on the Red X.
Cancel = Not ok2Close
End Sub
Part 2 of the code is located in module 1:
Code:
Public ok2Close As Boolean
Sub OVR_MEETING_1_Close()
'This code does the following tasks.
'1. Asks if you want the application closed.
'2. Returns the application to the home page (If yes).
'3. Does any final calculations & saves (If Yes).
'4. Closes both the work book & the application (if yes).
Dim Msg As String, Ans As Variant
Msg = "Would you like to close Career Link meeting List?"
Ans = MsgBox(Msg, vbYesNo, "Voc. Rehab - Career Link Data Entry")
Select Case Ans
Case vbYes
Dim i As Long
Application.ScreenUpdating = False
For i = 1 To ThisWorkbook.Sheets.Count
Application.Goto Reference:=Sheets(i).Range("A1"), Scroll:=True
Next i
Application.ScreenUpdating = True
Sheets("TOC").Select
Application.Calculation = xlCalculationAutomatic
ThisWorkbook.Close SaveChanges:=True
ok2Close = True
Application.Quit
End Select
End Sub