Hi,
I am wanting to make a macro to use Excel's "group" feature/tool as found in Data > Group but via a macro, examples of the function that I am wanting to use:
Columns("A:B").Columns.Group
I want to define columns based on a string such as:
GROUPCOLUMNS = Array("Title 4", "E 8", "E 4", "E 3", "E 2", "E 1")
For the first one in the list "Title 4" that one is a column on it's own which is easy to do.
The hard part is when it gets to the column titled "E 8" it needs to detect that this column is marked to be grouped, check the columns to the right until a column is no longer being "matched" in the GROUPCOLUMNS variable and hide them as a single group, but then when it cycles to the next column titled "E 4" it should skip this one as it's already been "grouped"
In my attached document, you can see the easy visual view of how it would group them by comparing Sheet 1 and Sheet 2
I'd prefer to stick with this sort of structure macro as someone else made as it's nice and easy to understand for me.
Sub GROUPING()
GROUPCOLUMNS = Array("Title 4", "E 8", "E 4", "E 3", "E 2", "E 1"
Set rng = Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row)
For Each a In rng.SpecialCells(xlCellTypeConstants).Areas
With a.CurrentRegion
Set r = .Rows(1)
For j = 1 To r.Columns.Count
'-----------------------------------------------------------------------------------------
If IsInArray(r.Cells(j).Value, GROUPCOLUMNS ) Then
' Not sure...
End If
'-----------------------------------------------------------------------------------------
Next j
End With
Next a
End Sub
Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
IsInArray = (UBound(Filter(arr, stringToBeFound)) > -1)
End Function
I'd greatly appreciate some advice.
Bookmarks