Hide/protect on close

JoshLyman

New Member
Joined
Jan 11, 2023
Messages
35
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Having some issues figuring out the right code (not super experienced). I would like, upon close, for all sheets to be protected (password 123), the workbook to be protected (password 123), and the tab called DASHBOARD to be hidden. Currently I have:

VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)

    Worksheets("DASHBOARD").Visible = False
    Dim xSheet As Worksheet
    Dim xPsw As String
    xPsw = "123"
    For Each xSheet In Worksheets
        xSheet.Protect xPsw
    Next
    ActiveWorkbook.Protect Password:="123", Structure:=True, Windows:=True

End Sub

Any help would be greatly appreciated!
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Hi Josh,

I changed it so the Dashboard would be very hidden. When protecting sheets it's best to specify what options you want to protect. I recorded a macro of me protecting one the sheets and then used that code as shown below. You're going to need to do the same.

VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    
    Dim xSheet As Worksheet
    Dim xPsw As String
    
    xPsw = "123"
    For Each xSheet In Worksheets
      If xSheet.Name = "DASHBOARD" Then
        xSheet.Visible = xlSheetVeryHidden                            '= xlSheetHidden
      End If
      xSheet.Protect Password:=xPsw, DrawingObjects:=False, Contents:=True, _
        Scenarios:=False, AllowSorting:=True, AllowFiltering:=True, _
        AllowUsingPivotTables:=True
    Next
    ThisWorkbook.Protect Password:="123", Structure:=True, Windows:=True

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,909
Messages
6,175,310
Members
452,634
Latest member
cpostell

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