Rin-Time Error '1004'

jimonfly

New Member
Joined
May 7, 2011
Messages
21
Just started getting this error today after several weeks of trying to finish a workbook I inherited. I tried several additions from help I received previously from the Board, but can't figure it out.

Unable to set the Hidden property of the Range class

Private Sub Worksheet_Activate()
Call SalesHideCol
End Sub
Sub SalesHideCol()
Dim Rng As Range
Dim c As Range
' Adjust the range to suit
Me.Unprotect Password:="Hermes"
Application.ScreenUpdating = False
Set Rng = Range("G1:K1")
For Each c In Rng
' Determine Criteria
If c.Value = "H" Then
c.EntireColumn.Hidden = True
Else
c.EntireColumn.Hidden = False
End If
Me.Protect Password:="Hermes"
Application.ScreenUpdating = False
Next c
End Sub

When I click Debug, the c.EntireColumn.Hidden = False row is highlighted.

The changes I made today are protecting the individual sheets with my password. Any ideas?

Thanks, Jim
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Code:
Sub SalesHideCol()

    Dim c      As Range
    
    ActiveSheet.Unprotect Password:="Hermes"
    Application.ScreenUpdating = False
    For Each c In Range("G1:K1") ' Adjust the range to suit
        ' Determine Criteria
        c.EntireColumn.Hidden = c.Value = "H"
    Next c
    Application.ScreenUpdating = True
    ActiveSheet.Protect Password:="Hermes"
    
End Sub
 
Upvote 0
Thanks, works perfectly. If its not too much trouble to ask, was the code I had incorrect, or just a more complicated route. It looks like yours is much more compact.

I didn't write the code, but it is incorporated in a similar fashion throughout. I'm not sure why the error popped up today as opposed to the last time I made changes.

Thanks again. Jim.
 
Upvote 0
In your original code, the line...
Code:
Me.Protect Password:="Hermes"
...is located within the For-Next loop. So you initially Unptotect the sheet then run the loop. Then on the first pass of the loop, the sheet is re-protected. On subsequent passes of the loop, you wouldn't be able to Hide\Unhide columns because the sheet is now protected causing an error.

If you move the line Me.Protect Password:="Hermes" after the line Next c, your original code should work.
 
Upvote 0

Forum statistics

Threads
1,224,606
Messages
6,179,866
Members
452,948
Latest member
UsmanAli786

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