Vba code to password protect workbook in Excel

I am trying to add a command button that will set a custom view, password protect all worksheets, and finally password protect the entire workbook structure. The code I am using will correctly set the view and add the password to the worksheets, but it will not apply the password to the workbook structure. What do I need to add?

Answer

According to Microsoft Help use

ActiveWorkbook.Protect Password:="test", Structure:=True, Windows:=True

(for formulas, depending on your country, you might have to change ; with , or the opposite

Other excel answers

 

 

You can find similar Excel Questions and Answer hereunder

1) How do I enter a formula in a cell using VBA (using Relative Reference)?

2) What is the view code button in the Excel Developer Tab

3) Write to text file without quotes in vba in Excel

4) How to use the trace error function in VBA

5) How can I delete all shapes in a WorkSheet?

6) How can I loop through all WorkSheets and get their names using VBA?

7) Calling a macro from another workbook in Excel

8) How can I activate a routine when there is a change in value of a cell?

9) I have a WorkBook that loads a form automatically when it is opened. In Excel, how can I suppress the form from loading on file open when required?

10) How to rename multiple sheets easily with VBA

 

Here the previous and next chapter