VBA password protect

  • Thread starter brian thompson3001 via OfficeKB.com
  • Start date
B

brian thompson3001 via OfficeKB.com

Hi

I have got this macro to run when opening. All works, but when I password
protect the sheet, and then close and open i get a run error.

I am not fluent with VBA and get by by recording and pasting other work off
the treads

Can anyone let me have the VBA code for protect / unprotect. and where it
should be entered in the code below . The password is "template"

thanks in advance

Private Sub auto_Open()
Sub averagesort()
Range("A6:AI36").Select
Selection.Sort Key1:=Range("C6"), Order1:=xlDescending, Header:=xlGuess,
_
OrderCustom:=1, MatchCase:=False, Orientation:=xlBottomToTop
ActiveWindow.ScrollRow = 6
Range("A3").Select
End Sub
 
E

ernestgoh

brian said:
Hi

I have got this macro to run when opening. All works, but when I
password
protect the sheet, and then close and open i get a run error.

I am not fluent with VBA and get by by recording and pasting other work
off
the treads

Can anyone let me have the VBA code for protect / unprotect. and where
it
should be entered in the code below . The password is "template"

thanks in advance

Private Sub auto_Open()
Sub averagesort()
Range("A6:AI36").Select
Selection.Sort Key1:=Range("C6"), Order1:=xlDescending,
Header:=xlGuess,
_
OrderCustom:=1, MatchCase:=False, Orientation:=xlBottomToTop
ActiveWindow.ScrollRow = 6
Range("A3").Select
End Sub

hi brian,

you may use this :

Private Sub auto_Open()

Worksheets("-Name of your worksheet-").unprotect password:="template"

Sub averagesort()
Range("A6:AI36").Select
Selection.Sort Key1:=Range("C6"), Order1:=xlDescending,
Header:=xlGuess,
_
OrderCustom:=1, MatchCase:=False, Orientation:=xlBottomToTop
ActiveWindow.ScrollRow = 6
Range("A3").Select

Worksheets("-Name of your worksheet-").protect password:="template"

End Sub

best regards, ernest goh
 
Z

Zone

if you are using the active sheet and not using a password, use
ActiveSheet.Protect
ActiveSheet.Unprotect

If you are using a password,
ActiveSheet.Protect password:="whatever"
ActiveSheet.Unprotect password:="whatever"
James
 
B

brian thompson3001 via OfficeKB.com

Hi Ernest

tried you alteration and the macro would not run. I copied and pasted into
"View Code" (right click on tab)

worksheet name "WTD"
Password "template"

any ideas why?


regards
brian said:
[quoted text clipped - 25 lines]
hi brian,

you may use this :

Private Sub auto_Open()

Worksheets("-Name of your worksheet-").unprotect password:="template"

Sub averagesort()
Range("A6:AI36").Select
Selection.Sort Key1:=Range("C6"), Order1:=xlDescending,
Header:=xlGuess,
_
OrderCustom:=1, MatchCase:=False, Orientation:=xlBottomToTop
ActiveWindow.ScrollRow = 6
Range("A3").Select

Worksheets("-Name of your worksheet-").protect password:="template"

End Sub

best regards, ernest goh
 
E

ernestgoh

Hi Brian

Try this as an example. Name one of your worksheet as "WTD". I teste
it out and it worked so you shouldn't have any problems.

Private Sub auto_Open()

Worksheets("WTD").Unprotect password:="template"

Sub averagesort()
Range("A6:AI36").Select
ActiveWindow.ScrollRow = 6
Range("A3").Select

Worksheets("WTD").Protect password:="template"

End Sub

Best regards, Ernest Goh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top