I have a workbook where I have inserted a command button to run a code to hide a range of cells then un-hide them when pressed again, that line works perfect. My problem is the workbook is protected so I added 2 lines of code to un-protect it and then re-protect it. how ever I get a runtime error 9 script out of range. When I debug it says my first line and un-protect line are bad. The first line works fine without the protection lines. This is what I have written. Do I need something before my un-protect line? I have written the un-protect and protect lines a couple different ways (sh.unprotect "david") and still get the same error. I am new to VBA and learning as I have been creating this workbook.
Private Sub CommandButton3_Click()
Sheets("Sheet2").Unprotect Password:="david"
Columns("O:T").Hidden = Not Columns("O:T").Hidden
Sheets("Sheet2").Protect Password:="david"
End Sub
Private Sub CommandButton3_Click()
Sheets("Sheet2").Unprotect Password:="david"
Columns("O:T").Hidden = Not Columns("O:T").Hidden
Sheets("Sheet2").Protect Password:="david"
End Sub