Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9315543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T02:23:41+00:00 2026-06-19T02:23:41+00:00

I have the code below that doesn’t seem to be working. Essentially, rngList refers

  • 0

I have the code below that doesn’t seem to be working. Essentially, rngList refers to a defined name range in Excel that is about 500 rows long and every n number of rows there is text (there are approximately 32 rows out of the 500 that have text). I am trying to go to the non-blank cells (by mimicking the ctrl + down command in Excel).

I am checking to see if they are blank, and if they are I want to group that cell. If it is not blank, I want to check the cell to the left and if it is 0, I also want to group it. The code I have now is essentially trying to do this but I am receiving the error below:

Group Method of Range Class Failed

It then goes on to highlight the following line:

Selection.Rows.Group

EDIT: Let’s say instead of grouping rows that are blank, I want to group rows that have 1 in them. That way the crtl + down will actually go to that cell rather than the last row.

Thank you very much for the help!

The code is below:

rngList.Cells(1).Select
    i = 0

    Do While i < 32
        i = i + 1
        If Selection.Value = "" Then
            Selection.Rows.Group
        Else
            Selection.End(xlToLeft).Select
                If Selection.Value <> 0 Then
                    Selection.Rows.ClearOutline
                End If
        End If
        Selection.End(xlToRight).Select
        Selection.End(xlDown).Select

    Loop
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T02:23:42+00:00Added an answer on June 19, 2026 at 2:23 am

    Despite the age of this post, I thought I’d throw in my two cents for anyone who might stumble upon it. I hope I understand your question correctly. Here’s what I’ve gathered:

    Goal: For every row in the column of interest, group rows based on a criteria.

    Criteria: The only rows in the group are those that either have no value (blank, null, empty) OR have a value AND have a neighboring cell (directly to the left) that has a value of 0. The only rows not in the group are those that are not blank and have a neighboring cell that is not 0.

    Here is some sample data:

    Note: the Range B1:B12 makeup the named range rngList, like the OP says they have.

    Data Before Running Macro:

    enter image description here

    Data After Running Macro – Grouping Uncollapsed:

    enter image description here

    Data After Running Macro – Grouping Collapsed:

    enter image description here

    The code that handles this:

    To make this code work: In the VBE (Visual Basic Editor), open the worksheet that contains the data to group (also contains the named range rngList) and paste this code, then run the macro.

    Note: The comments are added to explain certain parts in further detail, though I believe the code itself is written in a way that can explain itself (e.g. variable names are meaningful and logic makes sense).

    Public Sub GroupCells()
        Dim myRange As Range
        Dim rowCount As Integer, currentRow As Integer
        Dim firstBlankRow As Integer, lastBlankRow As Integer
        Dim currentRowValue As String
        Dim neighborColumnValue As String
    
        'select range based on given named range
        Set myRange = Range("rngList")
        rowCount = Cells(Rows.Count, myRange.Column).End(xlUp).Row
    
        firstBlankRow = 0
        lastBlankRow = 0
        'for every row in the range
        For currentRow = 1 To rowCount
            currentRowValue = Cells(currentRow, myRange.Column).Value
            neighborColumnValue = Cells(currentRow, myRange.Column - 1).Value
    
            If (IsEmpty(currentRowValue) Or currentRowValue = "") Then
                'if cell is blank and firstBlankRow hasn't been assigned yet
                If firstBlankRow = 0 Then
                    firstBlankRow = currentRow
                End If
            ElseIf Not (IsEmpty(currentRowValue) Or currentRowValue = "") Then
                'if the cell is not blank and its neighbor's (to the left) value is 0,
                'and firstBlankRow hasn't been assigned, then this is the firstBlankRow
                'to consider for grouping
                If neighborColumnValue = 0 And firstBlankRow = 0 Then
                    firstBlankRow = currentRow
                ElseIf neighborColumnValue <> 0 And firstBlankRow <> 0 Then
                    'if firstBlankRow is assigned and this row has a value with a neighbor
                    'who isn't 0, then the cell one row above this one is to be considered
                    'the lastBlankRow to include in the grouping
                    lastBlankRow = currentRow - 1
                End If
            End If
    
            'if first AND last blank rows have been assigned, then create a group
            'then reset the first/lastBlankRow values to 0 and begin searching for next
            'grouping
            If firstBlankRow <> 0 And lastBlankRow <> 0 Then
                Range(Cells(firstBlankRow, myRange.Column), Cells(lastBlankRow, myRange.Column)).EntireRow.Select
                Selection.Group
                firstBlankRow = 0
                lastBlankRow = 0
            End If
        Next
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have below code that reads file name after browse and sends it to
I have a program below that doesn't seem to be doing what I want
I have the code below. I'm wondering that does the self.value and _value have
I have the code below that hides and shows the navigational bar. It is
I have this javascript code below that uses jquery, it is suppoed to be
I have code that looks more or less like the code below but it
I have a piece of code (below) that can get the text of an
Background: I have a WPF UserControl (MainControl - not shown in code below) that
I have the code below on form so that the user submits the form
I have a plot (sample code pasted below) that I am trying to add

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.