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

lockunlock sheet and workbook

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

ResolvedVersion 2016

Coralie has attended:
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:

Select blank cells automatically

Get Excel to find any blank (empty) cells in a region for you by:

1. Selecting the appropriate region from your spreadsheet.

2. On the menu bar, go to Edit - Go to.

3. Click the 'Special' button, then select Blanks and click OK.

View all Excel hints and tips

Connect with us:

0207 987 3777

Call for assistance

Request Callback

We will call you back

Server loaded in 0.1 secs.