need a macro to group rows that contain '-'

tonyjyoo

Board Regular
Joined
Aug 5, 2016
Messages
167
I need a macro that groups rows that contain the '-' character (in column C:C)

Not hide, the + sign in Data -> group.
 
Last edited:

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Try this:

Code:
Sub groupRows()
    Dim row1 As Integer, row2 As Integer
    Dim colC As Integer
    row2 = 1
    colC = Cells(Rows.Count, 3).End(xlUp).Row
    Do
        row1 = row2
        While Cells(row1, 3).Value <> "-"
            row1 = row1 + 1
            If row1 > colC Then Exit Do
        Wend
        row2 = row1 + 1
        While Cells(row2, 3).Value = "-"
            row2 = row2 + 1
        Wend
        Range(Cells(row1, 3), Cells(row2 - 1, 3)).EntireRow.Group
    Loop
End Sub
 
Upvote 0
Try this:

Code:
Sub groupRows()
    Dim row1 As Integer, row2 As Integer
    Dim colC As Integer
    row2 = 1
    colC = Cells(Rows.Count, 3).End(xlUp).Row
    Do
        row1 = row2
        While Cells(row1, 3).Value <> "-"
            row1 = row1 + 1
            If row1 > colC Then Exit Do
        Wend
        row2 = row1 + 1
        While Cells(row2, 3).Value = "-"
            row2 = row2 + 1
        Wend
        Range(Cells(row1, 3), Cells(row2 - 1, 3)).EntireRow.Group
    Loop
End Sub

didnt work.

is it because my cells with - in column C are displaying it as zero as a formula?
 
Upvote 0
Could be. Go through the code step by step (F8) and when you're on a line with a "-" result, see what the value reads as in the code. Then change the <> "-" to that.
 
Upvote 0
In the workbook, select a cell that has the -. Then, in the VB window, type this into the Immediate Window (Ctrl+G) and hit Enter:

Code:
?"'" & Activecell.Value & "'"

What is the result in the Immediate window directly below the line you typed?

For example, in a cell that has the formula =Rand(), the value I get is '0.481588996523121' in the window.
 
Last edited:
Upvote 0
I figured it out, updated code because it was reading "0". Thanks.

One last thing, how can I adjust the macro to NOT group subtotal rows that have "0"?
 
Upvote 0
change the <> 0 and the = 0 around (e.g., change the <> 0 to = 0, and change the = 0 to <> 0).

I suppose you could instead add a NOT in each line rather than the above: While Not Cells(row1, 3).Value <> 0, etc.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,943
Messages
6,181,919
Members
453,071
Latest member
Gizmo2024

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top