+ Reply to Thread
Results 1 to 6 of 6

Loop to group data

Hybrid View

koticphreak Loop to group data 10-16-2012, 09:16 PM
koticphreak Re: Loop to group data 10-17-2012, 10:51 PM
cjo Re: Loop to group data 10-18-2012, 02:54 AM
watersev Re: Loop to group data 10-18-2012, 03:37 AM
cjo Re: Loop to group data 10-18-2012, 12:43 PM
koticphreak Re: Loop to group data 05-07-2014, 10:16 PM
  1. #1
    Forum Contributor
    Join Date
    09-13-2008
    Location
    Los Angeles, CA
    MS-Off Ver
    365
    Posts
    120

    Loop to group data

    I think I need a macro that is a for while loop that will group data for me...

    I basically have a number of reports that go like this:
    Blank line
    data 1
    data 2
    data 3
    data 4
    BOLD data
    Blank line
    data 5
    data 6
    data 7
    data 8
    BOLD data
    ...etc

    I basically would like to create a macro that groups all data and only leaves the 'BOLD data' visible when I collapse that grouping... so I would only see:
    Blank line
    ------------
    BOLD data

    With the "---" signifying the hidden (grouped) data lines. I feel like this is fairly easy to create, just not sure where to start.

    The pattern is consistent throughout the documents with only needing to look at column A for the bold/blank criteria, and it can stop as soon as it finds 2 consecutive blank lines.


    Thanks in advance for any help!

  2. #2
    Forum Contributor
    Join Date
    09-13-2008
    Location
    Los Angeles, CA
    MS-Off Ver
    365
    Posts
    120

    Re: Loop to group data

    No responses? I figured this would be easy... guess not

  3. #3
    Registered User
    Join Date
    05-30-2012
    Location
    St Paul, MN
    MS-Off Ver
    2010
    Posts
    51

    Re: Loop to group data

    Hi,
    Try this
    Sub GroupAtBolds()
        Dim i                As Long
        Dim lFirstInGroup    As Long
        Dim lLastInGroup     As Long
    
        i = 1
    
        Do While Not (Range("A" & i) = "" And Range("A" & i + 1) = "")
    
            'Test 1: see if the current line is blank.
            If Trim(Range("A" & i)) = "" Then
                lFirstInGroup = 0
                lLastInGroup = 0
            'test 2: see if the fontstyle is "Bold" (doing this second
            'in case the empty cell is formatted as "Bold", also)
            'Use the previous row as the last row in the group.
            ElseIf Range("A" & i).Font.FontStyle = "Bold" Then
                lLastInGroup = i - 1
            'if neither of the above conditions are true, and there isn't a
            'current FirstRow for the group found, then this row must be the
            'first row in the new group.
            ElseIf lFirstInGroup = 0 Then
                lFirstInGroup = i
            End If
            
            'If we have both a first row and last row identified for the group,
            'then we can group it.
            If lFirstInGroup <> 0 And lLastInGroup <> 0 Then
                Rows(lFirstInGroup & ":" & lLastInGroup).Group
            End If
            i = i + 1
        Loop
    
    End Sub

  4. #4
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Loop to group data

    hi koticphreak, option based on your example, run code "test"
    Attached Files Attached Files
    Last edited by watersev; 10-18-2012 at 03:43 AM.

  5. #5
    Registered User
    Join Date
    05-30-2012
    Location
    St Paul, MN
    MS-Off Ver
    2010
    Posts
    51

    Re: Loop to group data

    watersev,
    That's a nifty way of doing it.

  6. #6
    Forum Contributor
    Join Date
    09-13-2008
    Location
    Los Angeles, CA
    MS-Off Ver
    365
    Posts
    120

    Re: Loop to group data

    Hate to bring a post from the dead, but watersev's post was a huge help and totally worked. However, now the data has gotten more complicated, and there is sometimes now a blank row right before the bold - so now I need something that will always group based on the bold, not the blank row. Other than that, all else is the same! Thanks in advance.

    Also I will add (based on cjo's post) that the blank row is never set to bold, so no worry there.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.6.0 RC 1