View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
GS[_5_] GS[_5_] is offline
external usenet poster
 
Posts: 226
Default Grouping a range of sheets

What makes your post confusing is the terminology you're using with
respect to "grouping" sheets vs "grouping" columns/rows via
OutlineLevels. Firstly, if you already have a list of sheetnames that
you need to process outlines for then you don't need to 'group' sheets
to do this particular task. -You can just use a loop to process each
sheet in your list of sheetnames.

Example:
<snip
Dim avWks As Variant, i As Integer

avWks = Array("Sheet1", "Sheet2", "Sheet3")
For i = LBound(avWks) To UBound(avWks)
Sheets(avWks(i)).Outline.ShowLevels RowLevels:=1, ColumnLevels:=1
Next
</snip

If you want to group sheets programmatically so you can do something
with them 'as a group' then here's a reusable procedure that does that
in various ways.

' GroupSheets()
' This procedure requires only the necessary amount of coding be used
' in the Caller. By default, it requires passing only the first arg.
' Use Example: GroupSheets "Sheet1,Sheet3"
' creates a group of only those sheets.
' To group all sheets in a workbook except those sheets:
' GroupSheets "Sheet1,Sheet3", False
' You can pass the Wkb arg to specify any open workbook.
' (The Wkb doesn't need to be active for this purpose)
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''
Sub GroupSheets(sSheetnames As String, _
Optional bInGroup As Boolean = True, _
Optional Wkb As Workbook)
' Groups sheets in Wkb based on whether sSheetnames are to be included
' or excluded. Arg1 is a comma delimited string. (ie: "Sheet1,Sheet3")

Dim Shts() As String, sz As String
Dim i As Integer, wks As Worksheet, bNameIsIn As Boolean

i = 0: If Wkb Is Nothing Then Set Wkb = ActiveWorkbook

For Each wks In Wkb.Worksheets
bNameIsIn = (InStr(sSheetnames, wks.name) 0)
sz = "" '//init vars
If bInGroup Then
'Include sSheetname in group
If bNameIsIn Then sz = wks.name
Else
'Exclude sSheetname from group
If Not bNameIsIn Then sz = wks.name
End If
'Build the array
If Not sz = "" Then
ReDim Preserve Shts(0 To i): Shts(i) = sz: i = i + 1
End If
Next
'Select the array
ActiveWorkbook.Worksheets(Shts).Select
End Sub

To use it:
GroupSheets "Sheet1,Sheet2,Sheet3"

With ActiveWindow.SelectedSheets
'do stuff
End With

HTH

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc