VBA code to Lock Upon Saving

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
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Did some testing for you and the macro seems to me to be in working condition. When required it saves after changing a cell, with message if in range, without if not; works from menu button or closing the file, even if done over and over. Your issue must be somewhere else.
 
Upvote 0

Forum statistics

Threads
1,214,927
Messages
6,122,311
Members
449,080
Latest member
jmsotelo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top