VBA Grouping Macro needing to be modified

jpblev

New Member
Joined
Aug 27, 2015
Messages
3
I found a macro that almost gets me what I need and was wondering if someone would help to modify. The macro I have is grouping column A and leaving the first row of each HCode ungrouped. What I need is to group Column A leaving the last (Total) row of each HCode ungrouped:

-- corrupted image removed --

Here's the Macro that needs to be adjust:

VBA Code:
Sub GroupRows()
Dim myValue As Variant
Dim myCol As String
Dim myRow As Long
Dim startRow As Long
Dim lastRow As Long
'Which column to look at?
myCol = "A"
'Which row to look at first?
myRow = 2
lastRow = Cells(myRow, myCol).End(xlDown).Row
myValue = Cells(myRow, myCol)
startRow = myRow
Application.ScreenUpdating = False
For i = myRow + 1 To lastRow + 1
  If Cells(i, myCol).Value <> myValue Then
  'New group begins
Range(startRow + 1 & ":" & i - 1).EntireRow.Group
  startRow = i
  myValue = Cells(i, myCol).Value
  End If
  If myValue = "" Then
  'blank cell
Exit For
  End If
Next i
Application.ScreenUpdating = True
End Sub


Thanks so much for you help.

John
 
Last edited by a moderator:

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Just at a quick glance, maybe try changing this

Rich (BB code):
For i = myRow + 1 To lastRow + 1

to this:

Rich (BB code):
For i = myRow + 1 To lastRow - 1

to exclude the top and bottom rows. Or this:

Rich (BB code):
For i = myRow To lastRow - 1

to exclude only the last row and include the top row.
 
Last edited:
Upvote 0
Thanks but neither options gave me what I needed. I'm unable to upload/attach images here but, as an example:


Row 1 Col A x Col B x1
Row 2 Col A x Col B x2
Row 3 Col A x Col B x3
Row 4 Col A x Col B x total
Row 5 Col A y Col B y1
Row 6 Col A y Col B y2
Row 7 Col A y Col B y total

The current macro is grouping Rows 2,3,4 and then grouping Rows 6,7. I need to group Rows 1,2,3 and the group 5,6.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

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