VBA Frustration

Status
Not open for further replies.

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 I’d try here. I’ve been able to fix most of the compile errors but these 2 errors persist and I can’t 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:

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
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Duplicate https://www.mrexcel.com/forum/excel-questions/1047770-vba-troubles.html#post5030190

Please do not post the same question multiple times. All clarifications, follow-ups, and bumps should be posted back to the original thread. Per forum rules, posts of a duplicate nature will be locked or deleted (rule 12 here: Forum Rules).

If you do not receive a response, you can "bump" it by replying to it again, though we advise you to wait 24 hours before doing and not to bump a thread more than once a day.



 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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