huhwhatwho
New Member
- Joined
- Jan 12, 2018
- Messages
- 4
I have a workbook that I built a VBA code for to lock any cells where data has been entered each time the workbook is saved. The issue I'm having is that the code works upon the first save but not when its saved subsequent times. The workbook in question is a running log that calculates data based on entries that cannot be changed once entered for regulatory reasons. The data ranges are correct btw. Can one of you kind people help me figure out what's going on?
Option Explicit
Private bRangeEdited As Boolean
Private WithEvents ws As Worksheet
Private Sub Workbook_Open()
Set ws = Range("B5:I58").Parent
End Sub
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim sMSG As String
sMSG = "Prior to saving, ensure ALL data is correct. Once saved, any data entered cannot be edited." & vbLf
sMSG = sMSG & "Click YES to save. Click NO to continue editing."
If Not bRangeEdited Then GoTo Xit
If Not Me.ReadOnly Then
With Range("B5:I58")
If MsgBox(sMSG, vbExclamation + vbYesNo) = vbNo Then
Cancel = True
GoTo Xit
End If
.Parent.Unprotect "DATAbank3391"
If .SpecialCells(xlCellTypeBlanks).Address <> .Address Then
.SpecialCells(xlCellTypeConstants).Locked = True
bRangeEdited = False
End If
.Parent.Protect "DATAbank3391"
End With
End If
Xit:
End Sub
Private Sub ws_Change(ByVal Target As Range)
If Not Intersect(Range("B5:I58"), Target) Is Nothing Then
bRangeEdited = True
End If
End Sub
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Not Intersect(Range("B5:I58"), Target) Is Nothing Then
bRangeEdited = True
End If
End Sub
Option Explicit
Private bRangeEdited As Boolean
Private WithEvents ws As Worksheet
Private Sub Workbook_Open()
Set ws = Range("B5:I58").Parent
End Sub
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim sMSG As String
sMSG = "Prior to saving, ensure ALL data is correct. Once saved, any data entered cannot be edited." & vbLf
sMSG = sMSG & "Click YES to save. Click NO to continue editing."
If Not bRangeEdited Then GoTo Xit
If Not Me.ReadOnly Then
With Range("B5:I58")
If MsgBox(sMSG, vbExclamation + vbYesNo) = vbNo Then
Cancel = True
GoTo Xit
End If
.Parent.Unprotect "DATAbank3391"
If .SpecialCells(xlCellTypeBlanks).Address <> .Address Then
.SpecialCells(xlCellTypeConstants).Locked = True
bRangeEdited = False
End If
.Parent.Protect "DATAbank3391"
End With
End If
Xit:
End Sub
Private Sub ws_Change(ByVal Target As Range)
If Not Intersect(Range("B5:I58"), Target) Is Nothing Then
bRangeEdited = True
End If
End Sub
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Not Intersect(Range("B5:I58"), Target) Is Nothing Then
bRangeEdited = True
End If
End Sub