LowlyVBAUser
New Member
- Joined
- Mar 15, 2018
- Messages
- 4
I inherited a project from a ex-coworker and part of it involves Excel with a VBA code. I am not well versed in VBA...actually not versed at all and our IT department is useless so I figured I'd try a forum to see if anyone could help. I continue to get error messages from the code and no matter how much I read up on VBA, I can't seem to fix it. Please help!! The code is below
Code:
Option Explicit
Dim bEdited As Boolean, wksMain As Worksheet, wksSh As Worksheet
Const sSaveMsg$ = "Saving this workbook will lock and prevent editing of cells where data was entered." _
& vbLf & "(Choose YES to save, NO to continue editing)"
Const sWksMainName$ = "Almo"
Const sInputArea$ = "C6:G506"
Const sPWD$ = "mdcd"
Private Sub Workbook_Open()
Set wksMain = ThisWorkbook.Sheets(sWksMainName)
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Set wksMain = Nothing: Set wksSh = Nothing
End Sub
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Not bRangeEdited Then GoTo Xit
If Not Me.ReadOnly Then
With sInputArea
If MsgBox(sSaveMsg, vbExclamation + vbYesNo) = vbNo Then
Cancel = True: Exit Sub
End If
'Lock edits on all sheets
Dim v
For Each v In Me.Sheets
If Not v = wksMain Then
v.Unprotect "mdcd"
v.Range(gsInputArea).SpecialCells(xlCellTypeConstants).Locked = True
v.Protect "mdcd"
End If 'Not v = wksMain
Next 'v
End If 'Not Me.ReadOnly
bEdited = False
End Sub
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
bEdited = True: Set wksSh = Sh
End Sub