+ Reply to Thread
Results 1 to 5 of 5

Thread: Macro to Group Rows Along a Range Based on Values in a Column

  1. #1
    Registered User
    Join Date
    12-11-2008
    Location
    Southern California
    Posts
    13

    Macro to Group Rows Along a Range Based on Values in a Column

    I need a macro to group rows based on values in a specific column.

    I have a dashboard with data from row 21 to row 5000. The cells in column GM have the following values: "Hide", "Not Hide", or are blank. The data is sorted based on values in columns A, B, and C. As a result, there is no inherent order to the "Hide" or "Not Hide" values in the cells in column GM.

    I need a macro to go down column GM, and group any row where the value in column GM is "Hide". In some cases, only a single row will be grouped with a "+" sign. In other cases, consecutive rows will be grouped as such.

    I want to go with grouping rather than filtering because with grouping, data hidden along the range of the dashboard is more readily accessible.

    Any help would be greatly appreciated.

    Thank you very much.
    Last edited by JHCali; 05-23-2010 at 08:25 PM.

  2. #2
    Forum Guru
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,110

    Re: Macro to Group Rows Along a Range Based on Values in a Column

    JHCali;

    I'm curious; what do you mean by "Group"?
    Foxguy

    Remember to mark your questions [Solved] and rate the answer(s)
    Forum Rules are Here

  3. #3
    Registered User
    Join Date
    12-11-2008
    Location
    Southern California
    Posts
    13

    Re: Macro to Group Rows Along a Range Based on Values in a Column

    Quote Originally Posted by foxguy View Post
    JHCali;

    I'm curious; what do you mean by "Group"?
    Hi Foxguy,

    I am using Excel 2007. By group, I want the macro the accomplish what I would otherwise have to do by manually selecting individual or consecutive rows with a value of "Hide" in column GM, and going to the Data tab, selecting Group, then Group again. The Group feature in Excel ties a range of cells together so they can be expanded or collapsed.

  4. #4
    Forum Guru
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,110

    Re: Macro to Group Rows Along a Range Based on Values in a Column

    JHCali;

    I'm constantly learning things about Excel.
    Try This
    Sub Group_Hides()
        Dim rStart As Range, r As Range
        Dim lLastRow As Long, sColumn As String
        Dim rColumn As Range
    
        sColumn = "GM"
    
        lLastRow = Cells(Rows.Count, sColumn).End(xlUp).Row
        With ActiveSheet
            Set rColumn = .Range(.Cells(1, sColumn), Cells(lLastRow, sColumn))
            With rColumn
                Set r = .Cells(1, 1)
                Do Until r.Row > lLastRow
                    If rStart Is Nothing Then
                        If r.Value = "Hide" Then
                            Set rStart = r
                        End If
                    Else
                        If r.Value <> "Hide" Then
                            Range(rStart, r.Offset(-1, 0)).Rows.Group
                            Set rStart = Nothing
                        End If
                    End If
                    Set r = r.Offset(1, 0)
                Loop
            End With
        End With
    End Sub
    Foxguy

    Remember to mark your questions [Solved] and rate the answer(s)
    Forum Rules are Here

  5. #5
    Registered User
    Join Date
    12-11-2008
    Location
    Southern California
    Posts
    13

    Re: Macro to Group Rows Along a Range Based on Values in a Column

    Foxguy,

    Worked brilliantly! Thank you so much!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0