Error 1004 when Protected

jimonfly

New Member
Joined
May 7, 2011
Messages
21
I am adding sheets to an existing workbook containing Hidden Columns commands. The sheet works fine when it is unprotected, but when protected I get a "Run-time error '1004'" "Unable to set the Hidden property of the class"

The code is below:

Private Sub Worksheet_Activate()

Dim Rng As Range
Dim c As Range
' Adjust the range to suit
Set Rng = Range("a2:a18")


For Each c In Rng
' Determine Criteria
If c.Value = "H" Then
c.EntireRow.Hidden = True
Else
c.EntireRow.Hidden = False

End If
Application.ScreenUpdating = False
Next c
End Sub

When I hit "Debug", the line c.EntireRow.Hidden = False is highlighted in yellow.

Column A contains If Then statements regarding the number of Comparable Rentals used in an appraisal report, reporting either a "S" or "H" in Column A. This portion works fine, and the rows hide correctly when unprotected.

I copied the code above from another sheet, which works correctly protected or not, so it is not my code. However, I am trying to use it on several new sheets, and all give the error. Below is the original code:

Private Sub Worksheet_Activate()
Call SalesHideCol
Call SalesHideRows
End Sub
Sub SalesHideCol()
Dim Rng As Range
Dim c As Range
' Adjust the range to suit
Set Rng = Range("f1:J1")
For Each c In Rng
' Determine Criteria
If c.Value = "H" Then
c.EntireColumn.Hidden = True
Else
c.EntireColumn.Hidden = False

End If
Application.ScreenUpdating = False
Next c
End Sub
Sub SalesHideRows()
Dim Rng As Range
Dim c As Range
' Adjust the range to suit
Set Rng = Range("a3:a54")
For Each c In Rng
' Determine Criteria
If c.Value = "H" Then
c.EntireRow.Hidden = True
Else
c.EntireRow.Hidden = False

End If
Application.ScreenUpdating = False
Next c
End Sub

Does it have something to do with which cells are protected in the Format Cells box? I'm new and still learning VBA, so please bear with me.
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Try like this

Code:
Private Sub Worksheet_Activate()
Dim Rng As Range
Dim c As Range
' Adjust the range to suit
Me.Unprotect Password:="abc"
Application.ScreenUpdating = False
Set Rng = Range("a2:a18")
For Each c In Rng
' Determine Criteria
    c.EntireRow.Hidden = c.Value = "H"
Next c
Me.Protect Password:="abc"
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thanks. That worked great.

This may be too much to ask, but why did the code work on the original worksheet, but not on the one that I created? They were both protected and my code came directly from the original.

Jim
 
Upvote 0

Forum statistics

Threads
1,224,594
Messages
6,179,792
Members
452,942
Latest member
VijayNewtoExcel

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