0

I am new with VBA My worksheet works as normal with the command button. When i tried to protect my worksheet, right click on the worksheet tab and selected protect sheet with password. Once i completed it, i generated my command button in the worksheet, it showed error as below

enter image description here

I did not use any code for protection but right click on the worksheet and clicked on protect sheet. It worked to other sheets except the sheet I am using command button.

The code I used for the command button works well when the worksheet is not protected as below.

Sub Hide_Unhide()

    If Range("A4").Value = "Yes" Then
        Rows("4:4").EntireRow.Hidden = True
    Else
        Rows("4:4").EntireRow.Hidden = False
    End If
    
    If Range("A5").Value = "Yes" Then
        Rows("5:5").EntireRow.Hidden = True
    Else
        Rows("5:5").EntireRow.Hidden = False
    End If
    
End Sub
Darren Bartrup-Cook
  • 18,362
  • 1
  • 23
  • 45
Loan Le
  • 1
  • 1
  • Please include the code you are using in your question and explain which line gives you the error. – braX Jun 22 '20 at 07:09
  • 3
    Maybe if you unprotect the sheet, make whatever changes you need, and then re-protect it? – braX Jun 22 '20 at 07:12
  • I want to protect my worksheet to protect adding from other person and this worksheet is created for people to generate the CAP by command button " Re-fresh CAP". – Loan Le Jun 22 '20 at 07:47
  • The code i used for command button work well when the worksheet is not protected as below. And no code for protected, unprotected sheet sheet but right click on worksheet and choose protected sheet. Sub Hide_Unhide() If Range("A4").Value = "Yes" Then Rows("4:4").EntireRow.Hidden = True Else Rows("4:4").EntireRow.Hidden = False End If If Range("A5").Value = "Yes" Then Rows("5:5").EntireRow.Hidden = True Else Rows("5:5").EntireRow.Hidden = False End If End Sub – Loan Le Jun 22 '20 at 07:49
  • Please include the code in your question, not in a comment. As you can see, it just turns into a mess. You can edit your question... – braX Jun 22 '20 at 07:54
  • Have a look at [UserInterfaceOnly](https://learn.microsoft.com/en-us/office/vba/api/excel.worksheet.protect) when protecting the sheet. – Darren Bartrup-Cook Jun 22 '20 at 07:57
  • When you protect the sheet make sure you tick **Format Rows**. The user will be able to manually change the row height, or hide/unhide the row but it will do what you want without any extra coding. – Darren Bartrup-Cook Jun 22 '20 at 11:06
  • I don't know what error i am having now to fix it. Can anyone give me advice? my question is not resolved. And i did not used any code for protecting the sheet but use the protect sheet button in the excel to protect the sheet. The code does not work for the command button when i protect the sheet. – Loan Le Jun 23 '20 at 03:17

0 Answers0