Protecting workbook via VBA

cecil
StarLounger
Posts: 98
Joined: 09 Sep 2010, 16:01

Protecting workbook via VBA

Post by cecil »

I use the following code to protect a workbook. When I save the workbook, it is maximized. When I re-open the workbook, it is "restored" or part screen. I want this to alway be maximized. What am I missing?

Thanks.

Code: Select all

Sub HideAll()
    Dim ws As Worksheet
    Dim pswd As String
    
    pswd = "password"
    
    Menu.protect pswd, True, True
    
    For Each ws In ThisWorkbook.Worksheets
        If ws.Name <> Menu.Name Then
            ws.Visible = False
        End If
    Next
    
    ThisWorkbook.protect pswd, True, True
    
    
End Sub

User avatar
HansV
Administrator
Posts: 78549
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Protecting workbook via VBA

Post by HansV »

What is "Menu"?
Best wishes,
Hans

cecil
StarLounger
Posts: 98
Joined: 09 Sep 2010, 16:01

Re: Protecting workbook via VBA

Post by cecil »

Menu is a worksheet. I am hiding all worksheets except for one.

User avatar
HansV
Administrator
Posts: 78549
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Protecting workbook via VBA

Post by HansV »

Protecting the workbook windows causes this. To get around it, maximize the workbook window in the Workbook_Open event in the ThisWorkbook module:

Code: Select all

Private Sub Workbook_Open()
    Dim pswd As String
    pswd = "password"
    Me.Unprotect pswd
    Me.Windows(1).WindowState = xlMaximized
    Me.Protect pswd, True, True
End Sub
Best wishes,
Hans