LowlyVBAUser
New Member
- Joined
- Mar 15, 2018
- Messages
- 4
I inherited a project from an ex-coworker and part of it involves an Excel workbook with a VBA code. I am only just learning VBA and out IT department is useless so I figured Id try here. Ive been able to fix most of the compile errors but these 2 errors persist and I cant figure out how to correct it.
The 2 errors are as follows (see bolded):
The error points to this line (see bolded):
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Not bEdited Then
GoTo Xit (shows as Compile error: Label not defined)
If Not Me.ReadOnly Then
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(sInputArea).SpecialCells(xlCellTypeConstants).Locked = True
v.Protect "mdcd"
End If 'Not v = wksMain
Next 'v
End If 'Not Me.ReadOnly
bEdited = False
End Sub (shows as Compile error: Block If without End If)
The entire code is as follows:
The 2 errors are as follows (see bolded):
The error points to this line (see bolded):
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Not bEdited Then
GoTo Xit (shows as Compile error: Label not defined)
If Not Me.ReadOnly Then
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(sInputArea).SpecialCells(xlCellTypeConstants).Locked = True
v.Protect "mdcd"
End If 'Not v = wksMain
Next 'v
End If 'Not Me.ReadOnly
bEdited = False
End Sub (shows as Compile error: Block If without End If)
The entire code is as follows:
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$ = "Sheet 1"
Const sInputArea$ = "C6:G506"
Const sPWD$ = "Optrell2"
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 bEdited Then
GoTo Xit
If Not Me.ReadOnly Then
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 "Optrell2"
v.Range(sInputArea).SpecialCells(xlCellTypeConstants).Locked = True
v.Protect "Optrell2"
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