在VBA中对行进行分组

Max*_*234 7 excel vba excel-vba

我有下面的代码,似乎没有工作.本质上,rngList指的是Excel中定义的名称范围,大约500行,每n行数都有文本(500个中有大约32行有文本).我试图去非空白单元格(通过ctrl + down在Excel中模仿命令).

我正在检查它们是否是空白的,如果它们是我想要分组那个单元格.如果它不是空白,我想检查左边的单元格,如果它是0,我也想将它分组.我现在的代码基本上是尝试这样做,但我收到以下错误:

Group Method of Range Class Failed

然后继续强调以下行:

Selection.Rows.Group

编辑:让我们说,而不是将空行分组,我想分组其中包含1的行.这样,crtl + down实际上将转到该单元而不是最后一行.

非常感谢你的帮助!

代码如下:

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
Run Code Online (Sandbox Code Playgroud)

Sto*_*now 14

尽管这篇文章已经很久了,但我还是觉得我会为任何可能偶然发现它的人投入两美分.我希望我能正确理解你的问题.这是我收集的内容:

目标:对于感兴趣的列中的每一行,根据条件对行进行分组.

标准:唯一rows in the group没有值的值(空白,空,空)或者具有值并且具有值为0的相邻单元格(直接向左).只有rows not in the group那些空白且具有值不是 0 的相邻单元格.

以下是一些示例数据:

注意:范围B1:B12构成命名范围rngList,就像OP所说的那样.

运行宏之前的数据:

在此输入图像描述

运行宏后的数据 - 分组未折叠:

在此输入图像描述

运行宏后的数据 - 分组崩溃:

在此输入图像描述

处理这个的代码:

要使此代码工作:在VBE(Visual Basic编辑器)中,打开包含要分组的数据的工作表(还包含命名范围rngList)并粘贴此代码,然后运行宏.

注意:添加注释以更详细地解释某些部分,但我相信代码本身是以可以解释自身的方式编写的(例如,变量名称是有意义的,逻辑是有意义的).

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
Run Code Online (Sandbox Code Playgroud)