Code: Select all
Private Sub CommandButton1_Click()
MsgBox "You may now begin working on your report"
ActiveWorkbook.Protect Password:="", Structure:=False, Windows:=False
Dim wssheet As Worksheet
Application.ScreenUpdating = False
If CheckBox1.Value = True Then
Sheets("Sheet1").Visible = xlSheetVisible
Sheets("Sheet1").Protect DrawingObjects:=False, Contents:=True, Scenarios:=True, _
Password:="", UserInterfaceOnly:=True
End If
If CheckBox2.Value = True Then
Sheets("Sheet2").Visible = xlSheetVisible
Sheets("Sheet2").Protect DrawingObjects:=False, Contents:=True, Scenarios:=True, _
Password:="", UserInterfaceOnly:=True
End If
If CheckBox3.Value = True Then
Sheets("Sheet3").Visible = xlSheetVisible
Sheets("Sheet3").Protect DrawingObjects:=False, Contents:=True, Scenarios:=True, _
Password:="", UserInterfaceOnly:=True
End If
Application.ScreenUpdating = True
Worksheets("_").Visible = xlSheetHidden
For Each wssheet In ActiveWorkbook.Worksheets
If wssheet.Visible = xlSheetVisible Then
wssheet.Select
Exit For
End If
Next wssheet
ActiveWorkbook.Protect Password:="", Structure:=True, Windows:=True
Unload Me
Unload UserForm1
I then added
On Error GoTo Canceled
at the begining of code and
Canceled:
just before Workbook.Protect
this seems to get rid of the error but closes userform. So I then was trying to achieve a message saying “No checkbox was selected†if no ckeckbox was selected and for the userform not to unload and on the flip side I wanted a message saying “You may now begin working on your report†if any or all checkboxes are selected.
So I then added this code right after Next wssheet
Code: Select all
MsgBox ("You may now begin working on your report")
Canceled:
If Worksheets("_").Visible = True Then
MsgBox ("No checkbox was selected")
Exit Sub
End If
Code: Select all
Private Sub CommandButton1_Click()
ActiveWorkbook.Protect Password:="", Structure:=False, Windows:=False
Dim wssheet As Worksheet
On Error GoTo Canceled
Application.ScreenUpdating = False
If CheckBox1.Value = True Then
Sheets("Sheet1").Visible = xlSheetVisible
Sheets("Sheet1").Protect DrawingObjects:=False, Contents:=True, Scenarios:=True, _
Password:="", UserInterfaceOnly:=True
End If
If CheckBox2.Value = True Then
Sheets("Sheet2").Visible = xlSheetVisible
Sheets("Sheet2").Protect DrawingObjects:=False, Contents:=True, Scenarios:=True, _
Password:="", UserInterfaceOnly:=True
End If
If CheckBox3.Value = True Then
Sheets("Sheet3").Visible = xlSheetVisible
Sheets("Sheet3").Protect DrawingObjects:=False, Contents:=True, Scenarios:=True, _
Password:="", UserInterfaceOnly:=True
End If
Application.ScreenUpdating = True
Worksheets("_").Visible = xlSheetHidden
For Each wssheet In ActiveWorkbook.Worksheets
If wssheet.Visible = xlSheetVisible Then
wssheet.Select
Exit For
End If
Next wssheet
MsgBox ("You may now begin working on your report")
Canceled:
If Worksheets("_").Visible = True Then
MsgBox ("No checkbox was selected")
Exit Sub
End If
ActiveWorkbook.Protect Password:="", Structure:=True, Windows:=True
Unload Me
Unload UserForm1
End Sub