Group/Ungroup Macro

fedas18

Board Regular
Joined
Jan 21, 2010
Messages
81
Hi Everyone,

I am trying to write a macro that groups certain cells, 5:6 and 9:25. I then plan on putting a button to the right that says "show". WHen you click the button (run the macro), it will ungroup the column, if you click it again, it will group them.

Is this possible?

Thanks,
Evan
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Hello,

I am unsure if this is correct for you, this will show/hide entire rows 5:6 and 9:25

<font face=Courier New><SPAN style="color:#00007F">Public</SPAN> Trigger <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Boolean</SPAN><br><br><SPAN style="color:#00007F">Sub</SPAN> LevelShow()<br><br>    <SPAN style="color:#00007F">If</SPAN> Trigger = <SPAN style="color:#00007F">False</SPAN> <SPAN style="color:#00007F">Then</SPAN><br>        Sheets(1).Outline.ShowLevels rowlevels:=3<br>        Trigger = <SPAN style="color:#00007F">True</SPAN><br>    <SPAN style="color:#00007F">Else</SPAN><br>        Sheets(1).Outline.ShowLevels rowlevels:=1<br>        Trigger = <SPAN style="color:#00007F">False</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    <br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Upvote 0
I tested this on a new sheet and the above macro will not work if the groups are not made prior to using the above macro.

However, manually create your grouped rows first and the above posted macro should work...or use this macro one time:

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> GetGroup()<br><br>    Rows("5:6").Rows.Group<br>    Rows("9:25").Rows.Group<br><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Upvote 0
Thanks for getting back to me.

I grouped the columns and then used your first macro but it did not work :(

Wouldn't I need to specify the columns in the macro that I want to be grouped?



Right now I have columns 5:6 and 9:26 grouped. When I press this macro I wish that the columns expand and contract. Does that explain further?

Thanks for all of your help!
 
Upvote 0
I see. I am using excel 2007. It does work for me. Will you please test on a new workbook to see if it will work for you. Here are the steps:
  1. Open a New workbook.
  2. Alt + F11 Opens VBEditor
  3. Alt - I - M Keystorkes to insert module
  4. paste in both macros...make sure you also have the Public Trigger as Boolean line
  5. close the editor
  6. now insert a button and assign LevelShow macro to it
  7. Alt +f8
  8. run GetGroup
  9. Last, press the button a few times.
any change?
 
Upvote 0
It is a public variable. This means that all your procedures could use it... I am not sure if it the best method here. I could be wrong :) but I think using public it stores the value for the next time the macro is run. this "remembers" whether the rows are hide/unhide.

I hope the discription is basically right.. :eek:

Glad it worked,
Jeff
 
Upvote 0
Hello Repairman615,

Need some help... my query is slightly different to fedas18, thought you can help me with it..

I have got data in more than 1800 rows and applied 35 grouping's to it (rows count isn't same for these grouping's)

Now, I want to ungroup one at a time (of my choice) of 35 groups while sheet remain protected all the time...

If need be, i can put a number "1" of the selected column "A" above these grouping to ungroup specific group only

any advise on this would be greatly appreciated...




Thanks
mail2khalsa


Hello,

I am unsure if this is correct for you, this will show/hide entire rows 5:6 and 9:25

Public Trigger As Boolean

Sub LevelShow()

If Trigger = False Then
Sheets(1).Outline.ShowLevels rowlevels:=3
Trigger = True
Else
Sheets(1).Outline.ShowLevels rowlevels:=1
Trigger = False
End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,883
Messages
6,127,552
Members
449,385
Latest member
KMGLarson

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