Protect/unprotect certain cells with macro

1q2w3

New Member
Joined
Mar 20, 2020
Messages
24
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I need help with protecting certain cells within my worksheet to prevent cells from being edited.

I have an excel sheet that have a drop down list, it hides certain rows based on the selection. I used macros to set this up and these are working fine at the moment.

I have Column C and D that I want user to be able to edit, but E and F not.

I've tried using the protect sheet method but I am getting a run time error code 1004 - unable to set the hidden property of the range class.

Please can someone help?

Thanks
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
If the sheet is protected, unprotect it. Unlock all the cells that you want to be editable and lock the cells in columns E and F. Protect the sheet, with a password if you wish. At the beginning of your macro, enter a line of code to unprotect the sheet and at the end of the macro, enter another line to protect it again.
 
Upvote 0
Hi @mumps thanks for your reply.

What would the code be to unprotect/protect the sheet?

thanks
 
Upvote 0
Change the password (in red) to suit your needs.
Rich (BB code):
Sub test()
    ActiveSheet.Unprotect Password:="MyPassword"
    'your code here
    ActiveSheet.Protect Password:="MyPassword"
End Sub
 
Upvote 0
Can you please post your entire code?
 
Upvote 0
I had to rename the below, so e.g. I have to drop down box,

Drop down box 1 (Fruit or Veg) = 'Show All', 'Fruit', 'Veg'

Based on drop down box 1 then I have drop down box 2.

Drop down box 2 (Colour of item) = 'Green', 'Red', 'Other colours'

The code is below:


Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Dim Selection, Criteria As Range
Set Fruit or Veg = Range("A2")
Set Colour of item = Range("A4")

Select Case Fruit or veg
Case Is = "Show All": Rows("6:16").EntireRow.Hidden = False 'All rows are shown
Case Is = "Fruit": Rows("10:16").EntireRow.Hidden = True 'veg range is hidden
Rows("6:9").EntireRow.Hidden = False 'fruit range is shown
Case Is = "veg":
Rows("6:9").EntireRow.Hidden = True 'fruit range is hidden
Rows("10:16").EntireRow.Hidden = False 'veg range is shown

End Select

Select Case Colour of item
Case Is = "Green": Rows("9:16").EntireRow.Hidden = True 'Other colours are hidden
Rows("6:8").EntireRow.Hidden = False 'Green range are shown
Case Is = "Red": Rows("6:8").EntireRow.Hidden = True 'Other colours is hidden
Rows("12:16").EntireRow.Hidden = True 'Other colours range is hidden
Rows("9:11").EntireRow.Hidden = False 'Red range is shown

End Select

End Sub
 
Upvote 0
In which cells are the 2 drop down lists?
 
Upvote 0
Try:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("A2,A4")) Is Nothing Then Exit Sub
    ActiveSheet.Unprotect Password:="MyPassword"
    Application.ScreenUpdating = False
    Select Case Target.Address
        Case "$A$2"
            Select Case Target.Value
                Case "Show All"
                    Rows("6:16").EntireRow.Hidden = False
                Case "Fruit"
                    Rows("10:16").EntireRow.Hidden = True
                Case "veg"
                    Rows("6:9").EntireRow.Hidden = True
                     Rows("10:16").EntireRow.Hidden = False
            End Select
        Case "$A$4"
            Select Case Target.Value
                Case "Green"
                    Rows("9:16").EntireRow.Hidden = True
                    Rows("6:8").EntireRow.Hidden = False
                Case "Red"
                    Rows("6:8").EntireRow.Hidden = True
                    Rows("12:16").EntireRow.Hidden = True
                    Rows("9:11").EntireRow.Hidden = False
            End Select
    End Select
    ActiveSheet.Protect Password:="MyPassword"
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,455
Messages
6,124,937
Members
449,196
Latest member
Maxkapoor

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