Use VBA to insert password to lock sheets

Grimm127

Board Regular
Joined
Aug 17, 2011
Messages
132
Hello all you experts out there,

I have this code and it works for the most part except for the password. The code itself works but when I go to unlock the sheet it doesn't ask me for the password. So it doesn't seem to add it in. Can you please check it out and see what I am missing? THANK YOU in advance
Code:
Private Sub Workbook_Open()
    Dim wshTAB As Variant
    Dim intPassWord As String
    AccountPassWord = "dippy"


         For Each wshTAB In Worksheets(Array("NL_FTM_TRK", "NL_JAX_TRK", "NL_MIA_TRK", "NL_ORL_TRK", "NL_TAM_TRK", "NL_TRAN_OH", "RT_JAX_TRK", "RT_TAM_TRK"))
             wshTAB.Protect Password:=AccountPassWord
             wshTAB.EnableOutlining = True
             wshTAB.Protect Userinterfaceonly:=True
             wshTAB.Protect AllowFiltering:=True
         Next wshTAB


End Sub
 
Last edited by a moderator:

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Hi Grimm127,

See how this goes:

Code:
Option Explicit
Private Sub Workbook_Open()

    Dim wsMyTAB As Worksheet
    Dim varMyPW As Variant
    
    Application.ScreenUpdating = False
    
    varMyPW = "dippy"
    
    For Each wsMyTAB In Worksheets(Array("NL_FTM_TRK", "NL_JAX_TRK", "NL_MIA_TRK", "NL_ORL_TRK", "NL_TAM_TRK", "NL_TRAN_OH", "RT_JAX_TRK", "RT_TAM_TRK"))
        With wsMyTAB
            .EnableOutlining = True
            .Protect Userinterfaceonly:=True
            .Protect AllowFiltering:=True
            .Protect Password:=varMyPW
        End With
    Next wsMyTAB
    
    Application.ScreenUpdating = True

End Sub

Regards,

Robert
 
Upvote 0
SWEET!!!!!! God it looks Great and works perfect!

Now to unlock it would I just do this below?
Code:
Option Explicit
Private Sub Workbook_Open()

    Dim wsMyTAB As Worksheet
    Dim varMyPW As Variant
    
    Application.ScreenUpdating = False
    
    varMyPW = "dippy"
    
    For Each wsMyTAB In Worksheets(Array("NL_FTM_TRK", "NL_JAX_TRK", "NL_MIA_TRK", "NL_ORL_TRK", "NL_TAM_TRK", "NL_TRAN_OH", "RT_JAX_TRK", "RT_TAM_TRK"))
        With wsMyTAB
            .UnProtect Password:=varMyPW
        End With
    Next wsMyTAB
    
    Application.ScreenUpdating = True

End Sub
 
Last edited by a moderator:
Upvote 0
Having trouble with this. When I run it, it works fine it groups and ungroups. But the Filter doesn't work even if I unlock the cell.
When I close the file and reopen it, it will refuse to open the groups. Saying I have to unlock the sheet.....
 
Upvote 0
I'm confused as to what you're trying to do :confused:

On the one hand you want to lock the sheets in the array when you open the workbook and then you want to unlock them when you open the workbook? A sheet has to be unlocked if it is to be filtered.
 
Upvote 0
I'm confused as to what you're trying to do :confused:

On the one hand you want to lock the sheets in the array when you open the workbook and then you want to unlock them when you open the workbook? A sheet has to be unlocked if it is to be filtered.

Yes, one routine is to lock the sheets. To protect the integrity of the actual data when it's submitted to my Manager's so my roll-up perform as they should. Counterpoint, if there is an issue with one of my formulas, I would need to unlock all of them so I can correct it and then re lock it afterwards.
 
Upvote 0
I would put the macros in a separate standard module and then just call which you need, i.e. in a standard module have this...

Code:
Option Explicit
Public Const varMyPW As Variant = "dippy" 'Password
Private Sub ProtectSheets()

    Dim wsMyTAB As Worksheet
    
    Application.ScreenUpdating = False
    
    For Each wsMyTAB In Worksheets(Array("NL_FTM_TRK", "NL_JAX_TRK", "NL_MIA_TRK", "NL_ORL_TRK", "NL_TAM_TRK", "NL_TRAN_OH", "RT_JAX_TRK", "RT_TAM_TRK"))
        With wsMyTAB
            .EnableOutlining = True
            .Protect Userinterfaceonly:=True
            .Protect AllowFiltering:=True
            .Protect Password:=varMyPW
        End With
    Next wsMyTAB
    
    Application.ScreenUpdating = True

End Sub
Private Sub UnprotectSheets()

    Dim wsMyTAB As Worksheet
    
    Application.ScreenUpdating = False
    
    For Each wsMyTAB In Worksheets(Array("NL_FTM_TRK", "NL_JAX_TRK", "NL_MIA_TRK", "NL_ORL_TRK", "NL_TAM_TRK", "NL_TRAN_OH", "RT_JAX_TRK", "RT_TAM_TRK"))
        With wsMyTAB
            .Unprotect Password:=varMyPW
        End With
    Next wsMyTAB
    
    Application.ScreenUpdating = True

End Sub

...and then call (execute) whichever one you want form wherever you want like so:

Code:
Call ProtectSheets
Call UnprotectSheets

Regards,

Robert
 
Upvote 0
Exactly. Thanks for that. Do you know why when I save the file and close it. Reopen it they the grouping doesn't open any more? It says that the sheet is protected and I will need to un-protect to be able to use the group....
 
Upvote 0
In your original post the code you had to protect the desired sheets was assigned to the Workbook_Open workbook event. Any code here will run each time the workbook is opened. If you don't want this delete or comment it out.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,157
Members
453,021
Latest member
Justyna P

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