Run-time error '1004' - Macro for Hiding/Unhiding Columns

CameronC

New Member
Joined
Apr 18, 2014
Messages
3
I am trying to create macros to toggle between different views for an excel worksheet but keep getting "Run-time error '1004": Unable to set the Hidden property of the Range class. My goal is to use the same set of code but change the ranges for the hidden columns and unhidden columns. Below is the basic structure I am trying to use.


Code:
Sub Hidden_Column_View()

'Unprotect Worksheet
ActiveSheet.Unprotect

'Unhide Columns
Columns("A:HY").EntireColumn.Hidden = False

Range("A:J,M:O,Q:AL,AX:DB,EB:EG,FC:FF,FS:GH,GP:GU,HH:HY").EntireColumn.Hidden = Not Range("K:L,AM:AW,DC:EA,EH:FB,FG:FR,GI:GO,GV:HG,HZ:ID").EntireColumn.Hidden


'Protect Worksheet
ActiveSheet.Protect , AllowFiltering:=True


End Sub


I have tried many different iterations of the formula with no luck. Any assistance would be great! Thanks.
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Well, the first thing you do is unhide columns A:HY, then you run a check on a non-contiguous range containing those columns to see if it is hidden or not.

Columns A:HY are definitely visible (you've just made them visible) whereas columns HZ:ID may or may not be. Therefore the .EntireColumn.Hidden property would be ambiguous for that range, if such a property existed (it could be True for some columns and False for others, hence it could hold no single value of True or False)

So the logic is flawed.

Regardless of the above, even if HZ:ID were visible, you can't use that property on a range (contiguous or otherwise) - only on individual columns. (Think of it this way; what if your range had a specific number of rows rather than entire columns? You can't hide a column for some rows and not for others - hence that property is not allowed for ranges)

So I would define ranges and then loop through the individual columns within them, and hide / show them that way :

Code:
Dim rng As Range
Dim col As Variant

Set rng = ActiveSheet.Range("A:J,M:O,Q:AL,AX:DB,EB:EG,FC:FF,FS:GH,GP:GU,HH:HY")

For Each col In rng.Columns

    col.Hidden = True   ' Or False; depending on your criteria - you'll need to rethink this part...

Next col
 
Upvote 0
Not an expert by any means but this should toggle. How are you going to implement it though? Cant use a button as it will keep disappearing!

Code:
Sub Hidden_Column_View()
ActiveSheet.Unprotect
If Range("A:A").EntireColumn.Hidden = True Then
    Range("A:J,M:O,Q:AL,AX:DB,EB:EG,FC:FF,FS:GH,GP:GU,HH:HY").EntireColumn.Hidden = False
    Range("K:L,AM:AW,DC:EA,EH:FB,FG:FR,GI:GO,GV:HG,HZ:ID").EntireColumn.Hidden = True
    
Else
    
    Range("A:J,M:O,Q:AL,AX:DB,EB:EG,FC:FF,FS:GH,GP:GU,HH:HY").EntireColumn.Hidden = True
    Range("K:L,AM:AW,DC:EA,EH:FB,FG:FR,GI:GO,GV:HG,HZ:ID").EntireColumn.Hidden = False
    
End If
ActiveSheet.Protect , AllowFiltering:=True

Application.Goto Range("A1"), True
End Sub

Thinking about it two buttons may be ok.
 
Last edited:
Upvote 0
The goal is to be able to toggle between 8 different views of a worksheet using buttons in the sheet. Buttons are locked in place so they will not move with columns appearing/disappearing. For one of the buttons this code works for the macro but when I try to change the range for the different buttons it no longer works.

Code:
Sub Standard_View()

'Unprotect Worksheet
ActiveSheet.Unprotect

'Unhide All Columns
Columns("A:HY").EntireColumn.Hidden = False

'Hide Selection
Range("K:L,P:P,AM:AW,DC:EA,EH:FB,FG:FR,GI:GO,GV:HG").EntireColumn.Hidden = True


'Protect Worksheet
ActiveSheet.Protect , AllowFiltering:=True


End Sub
 
Upvote 0

Forum statistics

Threads
1,216,761
Messages
6,132,573
Members
449,736
Latest member
anthx

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