Running Macros across protected sheets.

Excelme at work

New Member
Joined
Feb 7, 2018
Messages
18
Hi there,

I recently created a calendar based spreadsheet with a drop down menu which lists, calendar months, and hides columns as necessary. Works perfectly.

However, I now need to share this document around so have protected all worksheets within, the problem then arises that if you select a month, you get the error message

Run time error 1004
Unable to set the hidden property of the range class.

The macro for the calendar is as follows;

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 4 And Target.Row = 1 Then
Dim c As Range, rng
Dim mymonth, month_val
month_val = Range("d1").Value


mymonth = Month(month_val)


Set rng = Range("e3:NJ3")
For Each c In rng
If Month(c) = mymonth Then
c.EntireColumn.Hidden = False
Else
c.EntireColumn.Hidden = True
End If
Next c
End If




End Sub

Would anyone happen to have a suitable workaround?
I have unticked "locked" on the entire column range that could potentially be hidden.
As soon as I unprotect the worksheet, the drop down list functions as expected.
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Upvote 0
Thanks I had hoped for an alternative.
You don't really need to add extra macros/procedures.
You should be able to just add the lines to unprotect/protect to your current code.
Those lines of code should have negligible effect on your code's performance (as long as you do not put them inside the loop so the run hundreds of times).
 
Upvote 0
You don't really need to add extra macros/procedures.
You should be able to just add the lines to unprotect/protect to your current code.
Those lines of code should have negligible effect on your code's performance (as long as you do not put them inside the loop so the run hundreds of times).

Hi thanks, could you explain more, I am more at a loss as to where the code should go, how it would be coded to only protect / unprotect one sheet?

I am relatively new to VBA this being the first time I have used more than one function simultaneously, learning as I go.
 
Upvote 0
Something like this (note that I also cleaned up your code a little bit):
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim c As Range, rng As Range
    Dim mymonth As Integer
    
    If Target.Column = 4 And Target.Row = 1 Then
[COLOR=#ff0000]        ActiveSheet.Unprotect "password"[/COLOR]
        mymonth = Month(Range("D1").Value)
        Set rng = Range("E3:NJ3")
        For Each c In rng
            If Month(c) = mymonth Then
                c.EntireColumn.Hidden = False
            Else
                c.EntireColumn.Hidden = True
            End If
        Next c
[COLOR=#ff0000]        ActiveSheet.Protect "password", True, True[/COLOR]
    End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

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