0

I've modified a VB code to show and hide rows based on the value of a cell. The code works well, but I need to password protect the sheet, and of course once the sheet is protected the VB won't run.

I've tried a few variations but I'm not a programmer so I'm just not getting it - could someone smarter please help me out?

Code as below:

Private Sub Worksheet_Change(ByVal Target As Range)

 Rows("1:" & Rows.Count).EntireRow.Hidden = False

 If Range("M11") = "0" Then
 Rows("13:92").EntireRow.Hidden = True

End If
 
 If Range("M11") = "Mini" Then
 Rows("13:18").EntireRow.Hidden = True
 Rows("38:57").EntireRow.Hidden = True
 
 End If

 If Range("M11") = "Small" Then
 Rows("13:18").EntireRow.Hidden = True
 Rows("43:57").EntireRow.Hidden = True
 
 End If

 If Range("M11") = "Standard" Then
 Rows("13:18").EntireRow.Hidden = True

 End If
 
End Sub

I tried variations of unlock code, but I don't know enough about programming to know how it all fits together, so I just get errors.

1
  • Use Worksheet.Protect and set UserInterfaceOnly = True. The macros will be able to make changes to the worksheet but it will be locked against user editing. Commented Nov 16, 2022 at 14:30

1 Answer 1

2

Unprotect, hide/unhide based on the cell value, then re-protect.

Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Me.Range("M11")) Is Nothing Then Exit Sub

    Me.Unprotect Password:="YourPasswordHere"
    Me.Rows.Hidden = False

    Select Case Me.Range("M11").Value
        Case 0
             Me.Rows("13:92").Hidden = True
        Case "Mini"
             Me.Rows("13:18").Hidden = True
             Me.Rows("38:57").Hidden = True
        Case "Small"
             Me.Rows("13:18").Hidden = True             
             Me.Rows("43:57").Hidden = True
        Case "Standard"
             Me.Rows("13:18").Hidden = True
    End Select

    Me.Protect Password:="YourPasswordHere"
End Sub

As mentioned in the comments, another option is to Protect the sheet using UserInterfaceOnly:=True. With this method, you'll need to leverage the Workbook_Open event to protect the sheet. UserInterfaceOnly does not persist if the workbook is closed.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, BigBen, but when I use this code nothing happens - the row hide functionality just completely stops.
In the Immediate Window, type ? Application.EnableEvents and hit Enter. What is the result? Did you also replace "YourPasswordHere" with your actual password?
When I add that code and hit Enter it adds "True" as a second line. I did replace the password, but nothing.
Next step might be to add some Debug.Print statements to see what code is executed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.