Code not working

ExcelNovice17

New Member
Joined
Apr 11, 2019
Messages
7
Hi,

I want my excel to hide and unhide a row when i select an option from my drop-down list but it is not working. My excel sheet needs to be protected and certain areas need to be locked including part of the row. Here is the code:

Private Sub Worksheet_Change(ByVal Target As Range)
If Range("I19") = "MALE" Then
Rows("35:36").Hidden = False
Else: Rows("35:36").Hidden = True
End If

End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
It can be condensed to 1 line which works for me:

Code:
Rows("35:36").Hidden = Range("I19") = "MALE"
 
Upvote 0
Try this:

Change "abc" to your sheet password

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Count > 1 Then Exit Sub
    If Target.Address(0, 0) = "I19" Then
        ActiveSheet.Unprotect "[COLOR=#ff0000]abc[/COLOR]"
        If Target.Value = "MALE" Then
            Rows("35:36").Hidden = False
        Else
            Rows("35:36").Hidden = True
        End If
        ActiveSheet.Protect "[COLOR=#FF0000]abc[/COLOR]"
    End If
End Sub

-----
Or this, taking Gallen's example

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Count > 1 Then Exit Sub
    If Target.Address(0, 0) = "I19" Then
        ActiveSheet.Unprotect "[COLOR=#ff0000]abc[/COLOR]"
        Rows("35:36").Hidden = Target.Value <> "MALE"
        ActiveSheet.Protect "[COLOR=#ff0000]abc[/COLOR]"
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,711
Messages
6,174,025
Members
452,542
Latest member
Bricklin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top