gordonc068
New Member
- Joined
- Feb 8, 2016
- Messages
- 12
I have some code that I took from a different Forum post which I've put below. I'm wanting to make it only apply to a specific worksheet names "New Hire Form". Can anyone adjust this do only happen if "New Hire Form" is the selected sheet? Also, do I put it in the "thisworkbook", Sheet, or Module section of the VBA application?
It is currently working, it checks a range called "Mandatory" and if they are all filled, then you can save or close, if not then it wont let you (forcing them to not send incomplete pages). There is a Skip it section in case you need to take a break in the middle of it, you are aware it's not done but you still need to save/close. Any help would be much appreciated.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Cancel = ForceDataEntry()
End Sub
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, _
Cancel As Boolean)
Cancel = ForceDataEntry()
End Sub
Private Function ForceDataEntry() As Boolean
If Range("Skipit") = "YES" Then
MsgBox ("As you wish")
Exit Function
End If
Dim rng As Range
Dim c As Variant
Dim rngCount As Integer
Dim CellCount As Integer
Set rng = Range("Mandatory")
rngCount = rng.Count
CellCount = 0
For Each c In rng
If Len(c) > 0 Then
CellCount = CellCount + 1
End If
Next c
ForceDataEntry = False
If CellCount <> rngCount Then
ForceDataEntry = True
MsgBox ("1. Check all highlighted feilds are complete or;" & vbNewLine & "2. See Cell E61")
End If
End Function
It is currently working, it checks a range called "Mandatory" and if they are all filled, then you can save or close, if not then it wont let you (forcing them to not send incomplete pages). There is a Skip it section in case you need to take a break in the middle of it, you are aware it's not done but you still need to save/close. Any help would be much appreciated.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Cancel = ForceDataEntry()
End Sub
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, _
Cancel As Boolean)
Cancel = ForceDataEntry()
End Sub
Private Function ForceDataEntry() As Boolean
If Range("Skipit") = "YES" Then
MsgBox ("As you wish")
Exit Function
End If
Dim rng As Range
Dim c As Variant
Dim rngCount As Integer
Dim CellCount As Integer
Set rng = Range("Mandatory")
rngCount = rng.Count
CellCount = 0
For Each c In rng
If Len(c) > 0 Then
CellCount = CellCount + 1
End If
Next c
ForceDataEntry = False
If CellCount <> rngCount Then
ForceDataEntry = True
MsgBox ("1. Check all highlighted feilds are complete or;" & vbNewLine & "2. See Cell E61")
End If
End Function