1

I am trying to create a macro that groups rows based on whether or not there is a value in column A. Some cells without a value may still have a null text string, so it would be best to use something like the length being greater than 2 as the condition for grouping rather than just blanks. The range for applying the macro would be row 3 through the last row of the data set (or if the range needs to be defined, through row 3000 would be sufficient). For example, if A4 had a value, and A10 had a value, then rows 5 through 9 should become a group. I found some code just Googling around, but I couldn't apply it right, so I'd rather just start from scratch. Thanks in advance!

fouraces
  • 59
  • 1
  • 2
  • 9
  • When you say "group" you mean merged? But then what indicates the end of the range where you don't want the cells merged anymore for that "group"? – ib11 May 16 '16 at 03:01
  • Also do you really need to "group" them? Is it not enough to just hide them or delete the rows? – ib11 May 16 '16 at 03:13
  • Not merged, the rows should be grouped (select multiple rows, go to the Data ribbon, and select the Group option on the right for an example). The end of the range is when the next value with LEN>2 occurs. If A4 value is Bobby, A5-A9 are blank, and A10 value is George, then rows 5-9 should be grouped, and this process should continue through row 3000 – fouraces May 16 '16 at 03:16
  • Yes, grouping is important, because it allows a single click for expand/collapse and also allows sub-groups to be made manually (there will still be values in other columns so we don't want to delete the rows – fouraces May 16 '16 at 03:17
  • Ashton's solution did it, thanks for all your help ib11 you've been ace – fouraces May 16 '16 at 03:25
  • Great, glad to hear. I saw it when I posted my code. – ib11 May 16 '16 at 03:30

2 Answers2

3

try this out works for me if the empty cells are blanks

sub ashGrp()

Dim rng As Range
Dim blankRange As Range
Dim grp As Range
Set rng = Range("a3", Cells(Rows.Count, 1).End(xlUp))
Set blankRange = rng.SpecialCells(xlCellTypeBlanks)

For Each grp In blankRange
    grp.Rows.Group
Next

end sub

if you need to group either text or blanks then this union code will do the trick

Sub ashGrp()

    Dim rng As Range
    Dim blankRange As Range
    Dim grp As Range
    Dim txtRange As Range
    Dim unionRange As Range

    Set rng = Range("a3", Cells(Rows.Count, 1).End(xlUp))
    Set blankRange = rng.SpecialCells(xlCellTypeBlanks)
    Set txtRange = rng.SpecialCells(xlCellTypeConstants, xlTextValues)
    Set unionRange = Union(blankRange, txtRange)

    For Each grp In unionRange
    grp.Rows.Group
    Next


End Sub
luckyguy73
  • 1,850
  • 2
  • 11
  • 21
2

You can try this. It is a narrowed down macro from this post: https://stackoverflow.com/a/14967281/6201755

Public Sub GroupCells()
    Dim myRange As Range
    Dim rowCount As Integer, currentRow As Integer
    Dim firstBlankRow As Integer, lastBlankRow As Integer
    Dim currentRowValue As String

    'select range based on given named range
    Set myRange = Range("A3:A3000")
    rowCount = Cells(Rows.Count, myRange.Column).End(xlUp).Row

    firstBlankRow = 0
    lastBlankRow = 0
    'for every row in the range
    For currentRow = 1 To rowCount
        currentRowValue = Cells(currentRow, myRange.Column).Value

        If (IsEmpty(currentRowValue) Or currentRowValue = "") Then
            'if cell is blank and firstBlankRow hasn't been assigned yet
            If firstBlankRow = 0 Then
                firstBlankRow = currentRow
            End If
        ElseIf Not (IsEmpty(currentRowValue) Or currentRowValue = "") Then
            If firstBlankRow <> 0 Then
                'if firstBlankRow is assigned and this row has a value
                'then the cell one row above this one is to be considered
                'the lastBlankRow to include in the grouping
                lastBlankRow = currentRow - 1
            End If
        End If

        'if first AND last blank rows have been assigned, then create a group
        'then reset the first/lastBlankRow values to 0 and begin searching for next
        'grouping
        If firstBlankRow <> 0 And lastBlankRow <> 0 Then
            Range(Cells(firstBlankRow, myRange.Column), Cells(lastBlankRow, myRange.Column)).EntireRow.Select
            Selection.Group
            firstBlankRow = 0
            lastBlankRow = 0
        End If
    Next
End Sub
Community
  • 1
  • 1
ib11
  • 2,530
  • 3
  • 22
  • 55
  • Cool. I did link in the original post, because that deserves an upvote, too. Also it is well commented, so you can learn from it. – ib11 May 16 '16 at 03:37