hide or unhide rows with button

B

Bea

I want to group rows together and have a quick expand/collapse button on the
sheet itself. I see the group/outline function but do not like the display of
the expand/collapse in the left side of the rows.
 
D

Dave Peterson

You could put a button from the control toolbox toolbar on that worksheet.
Double click on that button and you'll see where the code goes.

Paste this in that window and end up with something that looks like this.

Option Explicit
Private Sub CommandButton1_Click()
Dim myRng As Range

Set myRng = Me.Range("a3:a5,a7:a19,a22:a33")

myRng.EntireRow.Hidden = Not (myRng(1).EntireRow.Hidden)

End Sub

Adjust the range you want to hide/show

If you're new to macros, you may want to read David McRitchie's intro at:
http://www.mvps.org/dmcritchie/excel/getstarted.htm
 
B

Bea

This was perfect Dave! Thank you so much!

Dave Peterson said:
You could put a button from the control toolbox toolbar on that worksheet.
Double click on that button and you'll see where the code goes.

Paste this in that window and end up with something that looks like this.

Option Explicit
Private Sub CommandButton1_Click()
Dim myRng As Range

Set myRng = Me.Range("a3:a5,a7:a19,a22:a33")

myRng.EntireRow.Hidden = Not (myRng(1).EntireRow.Hidden)

End Sub

Adjust the range you want to hide/show

If you're new to macros, you may want to read David McRitchie's intro at:
http://www.mvps.org/dmcritchie/excel/getstarted.htm
 
G

Gord Dibben

Bea

There is also Views>Custom Views if you don't want to go the macro route.


Gord Dibben Excel MVP
 
B

Bea

Gord - Thanks for your help. Can you use a button with the Custom Views? I
could not figure that one out.
 
G

Gord Dibben

Bea

Not really.

You can go to Tools>Customize>Commands>View.

Drag the Custom Views drop-down onto your Toolbar.

You can choose which view from the drop-down.

Possible to have many custom views, each with a distinct name like "hidden"
"unhidden" "group1" etc.


Gord

Gord - Thanks for your help. Can you use a button with the Custom Views? I
could not figure that one out.
 
D

Dave Peterson

Sometimes you can just record a macro while you do it manually.

I got this when I showed the view named Test1.

Option Explicit
Sub Macro1()
ActiveWorkbook.CustomViews("test1").Show
End Sub


Behind a commandbutton from the control toolbox toolbar:

Option Explicit
Private Sub CommandButton1_Click()
Me.Parent.CustomViews("test1").Show
End Sub

Me is the worksheet that owns the code (and button). Me.Parent is the workbook.
Gord - Thanks for your help. Can you use a button with the Custom Views? I
could not figure that one out.
 
G

Gord Dibben

Dave

My custom views suggestion was only if Bea didn't want to use macros.

If using macros, better off to go with the toggle macro you first posted.


Gord Dibben Excel MVP
 
D

Dave Peterson

Ah, but your suggestion also works if Bea wants to set up the workbook the way
Bea wants and then use a button to show that view.

It might be a combination solution that works best for Bea.
 
G

Gord Dibben

I find it just as easy to drag the Custom Views drop-down to a Toolbar and
select a view from there.

But, that's me, not Bea.


Gord
 
D

Dave Peterson

Maybe...

You can hide rows if you use Data|Subtotals, or Data|Outlining or even
Data|Filter|autofilter.

But it really depends...
 
M

Mike

Is there a way to link the contents of a cell to this macro? For example, I
don't want to have a button, but I want to click the main category text/cell
to either hide or unhide the range specified... Is this possible?
 
D

Dave Peterson

You can hide entire rows or entire columns.

Or you can hide (kind of) a range by giving the a nice format--same color font
as the fill color (white on white???).

Or by using a custom number format:
;;;
(three semicolons)

If you need a macro, you could record one when you do it manually.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top