Forum Discussion
How to group automatically in excel
- May 25, 2022
Sekoleyte Interesting. So I found an easier and better way to do the WBS level:
=(SEARCH(LEFT(TRIM(B2),1),B2)-1)/2+1This version will NOT be affected by extra spaces at the end or in the middle 🙂
I also created a macro to automatically do the grouping based on whichever column you highlight:
Public Sub CreateGrouping() '(Optional c As Range) ', Optional UseSpaces = 0) Dim i, r, g As Integer Dim cc As Range If VarType(c) = vbObject Then Set cc = c Else Set cc = Selection End If r = Intersect(cc, ActiveSheet.UsedRange).Rows.Count 'use intersect incase they pass entire column For i = 1 To r If IsNumeric(cc.Cells(i, 1).Value2) Then g = cc.Cells(i, 1).Value2 j = i + 1 If cc.Cells(j, 1) > g Then Do While (cc.Cells(j + 1, 1).Value2 > g) And (j < r) j = j + 1 Loop Range(cc.Cells(i + 1, 1), cc.Cells(j, 1)).EntireRow.Group End If End If Next i End Sub
Sekoleyte Interesting. So I found an easier and better way to do the WBS level:
=(SEARCH(LEFT(TRIM(B2),1),B2)-1)/2+1This version will NOT be affected by extra spaces at the end or in the middle 🙂
I also created a macro to automatically do the grouping based on whichever column you highlight:
Public Sub CreateGrouping() '(Optional c As Range) ', Optional UseSpaces = 0)
Dim i, r, g As Integer
Dim cc As Range
If VarType(c) = vbObject Then
Set cc = c
Else
Set cc = Selection
End If
r = Intersect(cc, ActiveSheet.UsedRange).Rows.Count 'use intersect incase they pass entire column
For i = 1 To r
If IsNumeric(cc.Cells(i, 1).Value2) Then
g = cc.Cells(i, 1).Value2
j = i + 1
If cc.Cells(j, 1) > g Then
Do While (cc.Cells(j + 1, 1).Value2 > g) And (j < r)
j = j + 1
Loop
Range(cc.Cells(i + 1, 1), cc.Cells(j, 1)).EntireRow.Group
End If
End If
Next i
End Sub
Thank you so much. I could checked your solutions just now. I appreciate you for your approaches and solutions. I couldn't study on your macro yet but it works great! I like that kind of life saver solutions. I really respect you. Thank you for your help! Probably this will be my start to learning coding.
And thanks to all other helper guys!