Macro to Group andor Ungroup rows and/or columns

N

Nick Junod

Hello,
I'm an intermediate user of macros and VBA programing but I can't figure
this out. I want to create a macro that will identify if I have selected
rows or columns and if what I've selected is grouped or not and then either
group the selection or ungroup the selection based on these answers. This is
what I've built...obviously it doesn't work. Please help if you can.

Sub GroupUngroupSelection()

With selection
If Selection.Rows = True
If Selection.Rows.Group = True
selection.Rows.Ungroup
Else:
selection.Rows.Group
Elseif Selection.Columns = True
If Selection.Columns.Group = True
selection.Columns.Ungroup
Else:
selection.Columns.Group
End With
End Sub
 
J

Jim Cone

You need to explain what a grouped selection is.
--
Jim Cone
Portland, Oregon USA



"Nick Junod"
<[email protected]>
wrote in message
Hello,
I'm an intermediate user of macros and VBA programing but I can't figure
this out. I want to create a macro that will identify if I have selected
rows or columns and if what I've selected is grouped or not and then either
group the selection or ungroup the selection based on these answers.
This is what I've built...obviously it doesn't work. Please help if you can.

Sub GroupUngroupSelection()
With selection
If Selection.Rows = True
If Selection.Rows.Group = True
selection.Rows.Ungroup
Else:
selection.Rows.Group
Elseif Selection.Columns = True
If Selection.Columns.Group = True
selection.Columns.Ungroup
Else:
selection.Columns.Group
End With
End Sub
 
N

Nick Junod

What I mean by a grouped selection is the same thing as under the Data menu |
Group and Outline | Group.

Does that help?
 
J

Jim Cone

Sub WhichWay()
Dim rng As Range
Set rng = Selection
If rng.Columns.Count = Columns.Count Then
rng.Group
MsgBox "Entire Rows Grouped" & vbCr & rng.Address
ElseIf rng.Rows.Count = Rows.Count Then
rng.Group
MsgBox "Entire Columns Grouped" & vbCr & rng.Address
Else
MsgBox "Not entire rows or columns " & vbCr & _
"However let's group rows. " & vbCr & rng.Address
rng.EntireRow.Group
MsgBox "Rows grouped for... " & vbCr & rng.Address
End If
rng.EntireRow.ClearOutline
End Sub
--
Jim Cone
Portland, Oregon USA


"Nick Junod"
<[email protected]>
wrote in message
What I mean by a grouped selection is the same thing as
under the Data menu | Group and Outline | Group.

Does that help?
 

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