Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Need Excel button to hide/unhide columns

Posted on 2001-08-26
12
Medium Priority
?
828 Views
Last Modified: 2012-04-04
I want to give my users a way to switch between different ways to 'view' their worksheet.

Ideally I'd like a toolbar button that when clicked would hide certain columns, or if they are already hidden, unhide them.

Any help would be appreciated.

0
Comment
Question by:ksander
  • 4
  • 4
  • 2
  • +2
12 Comments
 
LVL 1

Expert Comment

by:sturobinson17
ID: 6426613
You could create 2 marco's. and two buttons. the first macro could be a recording of the columns being hidden, which would be played pack when button 1 is pressed. The second macro would be a recording of the columns being unhidden, which would be played back when button 2 is pressed. just an idea. :-)
0
 

Author Comment

by:ksander
ID: 6426623
That's sort of what I had in mind, except with one button and one macro - the macro would test, if the columns were hidden it would unhide, and vice versa.
0
 
LVL 22

Expert Comment

by:ture
ID: 6426661
ksander,

Perhaps you should consider a non-VBA approach for this.

Take a look at the View manager in the View (perhaps that is not the exact wording - I'm on a Swedish version of Excel now)

It is also possible to add an easy-to-use dropdown to help switching between views. Tools - Customize - Commands tab, In the Views category, drag the "View manager" listbox to any of your toolbars.

Ture Magnusson
Karlstad, Sweden
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 5

Expert Comment

by:jklmn
ID: 6426662
Hi ksander,

Assume you need to hide/unhide column 2, try put following code to your button event:

    With Sheet1.Columns(2).EntireColumn
    .Hidden = Not .Hidden
    End With
0
 
LVL 13

Expert Comment

by:cri
ID: 6426792
If you do not have too many of these columns, use the Data|Group&Outline feature. If your worksheet is protected (as any distributed workbook should be), there is way to enable the group/ungroup on a protected worksheet. If interested, please ask.
0
 

Author Comment

by:ksander
ID: 6427075
jklmn - It doesn't work ("unable to set the hidden property of the range class") because the sheet is protected.

When I unprotect the sheet nothing happens when I call the macro as written.

I tried replacing "Sheet1" with the true sheet name but get an "object required" error.  The macro should work on whatever the current sheet is.

But I guess the main thing is that it must work while the sheet is protected.  
-----------------------
ture - I did that and it works but not well.
1. Wherever it's called, the 'view' I created insists on jumping to it's 'home' sheet. I need it to show the same view (hiding the same columns) in all sheets.
2. If the workbook is closed and saved while a sheet is in a custom view, ir reopens in the same view.  It want to return to 'normal'.
-------------------------
cri - if Data/Group&Outline is the route to take, please tell me more.
--------------------------

One thing that may be a problem with what I want is that the I put an icon or control on the Web bar ,the only one visible, it's gone next time I open the sheet.

0
 
LVL 5

Expert Comment

by:jklmn
ID: 6427134
Hi ksander,

The code is working in my Excel sheet with no protected, it should work for you as well.....but you need the sheet protected.....I got no idea at the moment :-(
0
 
LVL 22

Expert Comment

by:ture
ID: 6427810
ksander,

If your code is run from a button on a worksheet, you may need to activate the active cell (kind of stupid) to avoid errors.

To hide/unhide columns with a protected sheet, you must unprotect it first. The best way is to protect it with the option UserInterfaceOnly:=True. This way, your code can work with the sheet but it is still protected from the user.

Sub View1()
 
  'Make sure that a cell is active
  ActiveCell.Activate
   
  'Protect the sheet only for manual editing.
  'The sheet can still be edited by VBA code.
  ActiveSheet.Protect Password:="carrot", UserInterfaceOnly:=True
 
  'Unhide all columns
  ActiveSheet.Columns.Hidden = False
 
  'Hide some of the columns
  ActiveSheet.Range("A:A,C:H,AA:AZ").EntireColumn.Hidden = True

End Sub

Sub View2()
 
  'Make sure that a cell is active
  ActiveCell.Activate
   
  'Protect the sheet only for manual editing.
  'The sheet can still be edited by VBA code.
  ActiveSheet.Protect Password:="carrot", UserInterfaceOnly:=True
   
  'Unhide all columns
  ActiveSheet.Columns.Hidden = False
 
  'Hide some of the columns
  ActiveSheet.Range("J:Z").EntireColumn.Hidden = True

End Sub

Ture Magnusson
Karlstad, Sweden
0
 

Author Comment

by:ksander
ID: 6428080
Sorry ture, I don't understand.
 
1) View1 & View2 - are they identical except for which columns are hidden ?
2) Are you saying to use View1 to hide one group of columns and View2 to hide another - each with its own button ?
3) I don't see any sort of a toggle or an If test to see if the routine needs to hide or unhide.
4) I don't understand why the Protect statement is in the same code to hide or unhide columns. Assuming the sheet is already protected (as it is with an Auto_Open), is it necessary to re-protect with UserInterface.=True ? Or could I include that statement in Auto_Open.

Thanks

 
0
 
LVL 22

Accepted Solution

by:
ture earned 400 total points
ID: 6428367
1) Yes.
2) Yes.
3) Correct. My procedures don't have a toggle or check for hidden/unhidden columns.
4) It would be good to put the protection, with UserInterfaceOnly:=True, in an Auto_Open procedure.

In the procedure below, I have removed the protect statement, as I assume that you have it in your Auto_Open procedure. I have also added "toggle functionality" to switch between hiding/unhiding columns.

Sub ToggleView()

 'Declare a variable
 Dim IsHidden As Boolean

 'Make sure that a cell is active
 ActiveCell.Activate
   
 'Check Hidden state of one of the columns
 IsHidden = ActiveSheet.Range("C:C").EntireColumn.Hidden

 'Change hidden state of some columns
 ActiveSheet.Range("C:H,AA:AZ").EntireColumn.Hidden = Not IsHidden

End Sub

/Ture
0
 

Author Comment

by:ksander
ID: 6428841
Perfect - thanks very much
0
 
LVL 22

Expert Comment

by:ture
ID: 6429016
You are welcome, ksander! Glad it worked!

/Ture
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Cancel future meetings from user mailboxes in Office 365 using Remove-CalendarEvents
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question