Group By Issue

f00dFights

New Member
Joined
Jul 12, 2022
Messages
12
Office Version
  1. 365
Platform
  1. Windows
Hi,
ive been working on a group by function however ive run into an issue, because when the macro comes across a sheet with one row then it throws an exception at the commented line below when really it should escape if it cant group a row.

Im sure its probably an easy fix but ive tried a few workarounds such as elseif conditions but I can't quite find a fix

VBA Code:
  Dim gruRng As Range
  Dim gruVari As Variant
  Dim i As Long, j As Long

  With ActiveSheet
        On Error Resume Next
        ' expand all groups on sheet
        .Outline.ShowLevels RowLevels:=8
        ' remove any existing groups
        .Rows.Ungroup
        On Error GoTo 0
        Set gruRng = .Range("T4", .Cells(.Rows.Count, 2).End(xlUp))
    End With
   
    With gruRng
        'identify common groups in column S (Job No)
        j = 1
        gruVari = .Cells(j, 1).Value
       
        For i = 2 To .Rows.Count
           If gruVari <> .Cells(i, 1) Then
             ' Column S (Job No) has changed, create group
              gruVari = .Cells(i, 1)
              If i > j + 1 Then
                 .Cells(j + 1, 1).Resize(i - j - 1, 1).Rows.Group ' <- Throws exception here: Application-defined or object defined error
              End If
              j = i
              gruVari = .Cells(j, 1).Value
           End If
        Next
       
         'create last group
         If i > j Then
            .Cells(j + 1, 1).Resize(i - j - 1, 1).Rows.Group
         End If
        ' collapse all groups
        .Parent.Outline.ShowLevels RowLevels:=1
    End With
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Have you chosen multiple worksheets at once? In this situation, groupings cannot be added. Only sheet by sheet will do. Choose just one worksheet, then.
 
Upvote 0
Have you chosen multiple worksheets at once? In this situation, groupings cannot be added. Only sheet by sheet will do. Choose just one worksheet, then.
im only choosing one sheet at a time, the code is part of a refresh function which is used across the whole project (so when the macro is executed, the selected sheet is reorganised, regrouped etc)
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,119
Members
452,381
Latest member
Nova88

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