lockunlock sheet and workbook

Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Lock/unlock sheet and workbook with a password in a Macro

Lock/unlock sheet and workbook with a password in a Macro

resolvedResolved · Medium Priority · Version 2016

Coralie has attended:
Excel Intermediate course
Excel Advanced course

Lock/unlock sheet and workbook with a password in a Macro

Good morning,

I have an Excel Spreadhseet for which I have the following macro:

- Name: Lock_Sheet_Workbook
- Recording:

Sub lock_sheet()
'
' lock_sheet Macro
'
' Keyboard Shortcut: Ctrl+Shift+L
'
Sheets("sheet1").Select
ActiveSheet.protect DrawingObjects:=True, Contents:=True, Scenarios:=True
Sheets("sheet2").Select
ActiveSheet.protect DrawingObjects:=True, Contents:=True, Scenarios:=True
ActiveWorkbook.protect Structure:=True, Windows:=False
End Sub

>>>> WHAT I WANT TO DO is:
Lock sheet1 with password "ABC", Lock sheet2 with password "ABC", lock workbook with password "ABC"

My Question to you: How and where can I write this in my VBA?

Many thanks,
Coralie

RE: Lock/unlock sheet and workbook with a password in a Macro

Hi Coralie,

Thank you for the forum question.

The code in your macro will protect the workbook.

The VBA code will do the job:
Sub ProtectMyworkbook()

ThisWorkbook.Protect Password:="myPassword"

End Sub

If you only want to protect a spreadsheet use the code below. You will need to change "Sheet1" to the name of the worksheet you want to protect:

Sub ProtectSheetWithPassword()


Sheets("Sheet1").Protect Password:="myPassword"

End Sub


Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

RE: Lock/unlock sheet and workbook with a password in a Macro

Sorry

I only answered half of the question.

You will have to write the code in the visual basic editor.

Press ALT F11 (if this is not opening the visual basic editor press ALT FN F11. On some laptops you will need to use FN to be able to use the F keys).

In the visual basic editor click INSERT MODULE at the top.

Type the code in the module. You will need to save the file as a MACRO ENABLE WORKBOOK in the Save As dialog box. The extension for a macro enable workbook is XLSM instead of the "normal" extension XLSX.


Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

RE: Lock/unlock sheet and workbook with a password in a Macro

Hi, thank you, all works

Many thanks,
Coralie

RE: Lock/unlock sheet and workbook with a password in a Macro

You are welcome.


Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

 

Training courses

 

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.


 

Excel tip:

Turn Function tooltips on and off

Excel 2002 (XP) and Excel 2003 have the Function tooltips facility. When you type in a function name followed by a bracket, for example, =IF(, a yellow box appears beside the function name and lists the function's arguments. This is very useful when you can't quite remember the order of a function's arguments or what the arguments actually are!

However, Function tooltips can become annoying. To turn them off, choose Tools|Options. and select the General tab. Then, untick the Function tooltips box and choose OK.

View all Excel hints and tips


Server loaded in 0.08 secs.