You are on page 1of 5

-1Back to: Excel VBA . Got any Excel/VBA Questions?

Free Excel Help Excel has protection that we can add to an Excel Worksheet via Tools>Protection>Protect Sheet. A password can also be supplied so that another user cannot unprotect it without the password. A common question that I'm often asked is, "how can I password protect and unprotect all Worksheets in my Workbook in one go. I have many sheets and I have to protect and unprotect each Worksheet individually." Unfortunately there is no standard feature in Excel that will allow us to protect and unprotect all Worksheets in one go, however, we can use some fairly simple VBA code to do this for us. Follow the steps below. 1. Open the Workbook the code is needed for. Or, go to Window>Unhide and unhide your Personal.xls to make it available to any Workbook. 2. Go to Tools>Macro>Visual Basic Editor (Alt+F11) and then Insert>UserForm. This should also display the Control Toolbox. If it doesn't go to View>Toobox. 3. From the Toolbox select a TextBox (ab|). Now click onto the UserForm to add it. Position it in the top left and size it so it is your preferred size. 4. Ensure the Textbox is still selected and then go to View>Properties (F4). From the Properties Window of the Textbox scroll down to PasswordChar and in the white box on the right enter an asterisk * 5. Now from the Toolbox select a CommandButton and then click the UserForm and position it in the top right. 6. With the CommandButton still selected go to View>Properties (F4). From the Properties Window of the CommandButton scroll down to Caption and in the white box on the right enter the caption: OK. If you are using Excel 97 also scroll down to TakeFocusOnClick and set this to False. 7. Now select the UserForm and from it's Properties Window find Caption and change it to: Protect/Unprotect all sheets. Medicine

9. Now go to View>Code (F7) and place in the code exactly as shown below

10. Private Sub CommandButton1_Click() 11. 12. Dim wSheet As Worksheet 13. 14. For Each wSheet In Worksheets 15. 16. If wSheet.ProtectContents = True Then 17. 18. wSheet.Unprotect Password:=TextBox1.Text 19. 20. Else 21. 22. wSheet.Protect Password:=TextBox1.Text 23. 24. End If 25. 26. Next wSheet 27. 28. Unload me 29. End Sub

30. Now go to Insert>Module and in here add the code below


31. Sub ShowPass() 32. 33. UserForm1.Show 34. End Sub

35. Now click the top right X, or push Alt+Q to get back to Excel. 36. Go to Tools>Macro>Macros (Alt+F8) select ShowPass and then click Options and assign a shortcut key.

This newly added feature will unprotect all Worksheets that are protected and protect all Worksheets that are unprotected. A WORD OF WARNING: As you are not asked to confirm your password you should be very sure of what you type.

-2-

Sub PasswordBreaker() 'Author unknown but submitted by brettdj of www.expertsexchange.com Dim i As Integer, j As Integer, k As Integer Dim l As Integer, m As Integer, n As Integer Dim i1 As Integer, i2 As Integer, i3 As Integer Dim i4 As Integer, i5 As Integer, i6 As Integer On Error Resume Next For i = 65 To 66: For j = 65 To 66: For k = 65 To 66 For l = 65 To 66: For m = 65 To 66: For i1 = 65 To 66 For i2 = 65 To 66: For i3 = 65 To 66: For i4 = 65 To 66 For i5 = 65 To 66: For i6 = 65 To 66: For n = 32 To 126

ActiveSheet.Unprotect Chr(i) & Chr(j) & Chr(k) & _ Chr(l) & Chr(m) & Chr(i1) & Chr(i2) & Chr(i3) & _ Chr(i4) & Chr(i5) & Chr(i6) & Chr(n) If ActiveSheet.ProtectContents = False Then MsgBox "One usable password is " & Chr(i) & Chr(j) & _ Chr(k) & Chr(l) & Chr(m) & Chr(i1) & Chr(i2) & _ Chr(i3) & Chr(i4) & Chr(i5) & Chr(i6) & Chr(n) ActiveWorkbook.Sheets(1).Select Range("a1").FormulaR1C1 = Chr(i) & Chr(j) & _ Chr(k) & Chr(l) & Chr(m) & Chr(i1) & Chr(i2) & _ Chr(i3) & Chr(i4) & Chr(i5) & Chr(i6) & Chr(n) Exit Sub End If Next: Next: Next: Next: Next: Next Next: Next: Next: Next: Next: Next

End Sub

-3-

Visual Basic
C#
private void WorkbookUnprotect() { MessageBox.Show("Protecting the workbook."); this.Protect(missing, true, true); if (DialogResult.Yes == MessageBox.Show("Unprotect the workbook?", "Custom Unprotect Dialog", MessageBoxButtons.YesNo)) { this.Unprotect(missing); } }

You might also like