8

我有下面的代码似乎不起作用。本质上,rngList是指 Excel 中定义的名称范围,大约 500 行长,每n行都有文本(500 行中大约有 32 行有文本)。我正在尝试转到非空白单元格(通过模仿ctrl + downExcel 中的命令)。

我正在检查它们是否为空白,如果它们是我想对该单元格进行分组。如果它不是空白的,我想检查左边的单元格,如果它是 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
4

2 回答 2

15

尽管这篇文章的年代久远,但我想我会为任何可能偶然发现它的人投入两分钱。我希望我能正确理解你的问题。这是我收集的内容:

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

标准:只有rows in the group那些没有值(空白、空、空)或有一个值并且有一个值为 0 的相邻单元格(直接在左边)。唯一rows not in the group的是那些不是空白的并且有一个为0 的相邻单元格。

以下是一些示例数据:

注意: RangeB1:B12构成了命名的 range rngList,就像 OP 所说的那样。

运行宏前的数据:

在此处输入图像描述

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

在此处输入图像描述

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

在此处输入图像描述

处理这个的代码:

要使此代码工作:在 VBE(Visual Basic 编辑器)中,打开包含要分组的数据的工作表(也包含命名的 range 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
于 2013-02-19T20:58:21.400 回答
1

我使用 Sam 的代码进行分组而不使用 A 列。我认为其他人会发现它很有用。

Sub Group_Jobs()

Dim myRange As Range
Dim rowCount As Integer, currentRow As Integer
Dim firstBlankRow As Integer, lastBlankRow As Integer
Dim currentRowValue As String
Dim nextRowValue As String

Application.ScreenUpdating = False 'Stop screen updating while grouping

'select range based on given named range
Set myRange = Range("A1:A1000")
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
    nextRowValue = Cells(currentRow + 1, myRange.Column).Value

'Assign firstBlankRow & lastBlankRow
    'if currentRowValue = NotBlank(Job#) And nextRowValue = NotBlank(Job#) Then Skip
    'if currentRowValue = Blank          And nextRowValue = Blank          Then Skip
    'if currentRowValue = NotBlank(Job#) And nextRowValue = Blank          Then is firstBlankRow
    'if currentRowValue = Blank          And nextRowValue = NotBlank(Job#) Then is lastBlankRow
    If Not (currentRowValue = "" Or currentRowValue = "") Then
        If (IsEmpty(nextRowValue) Or nextRowValue = "") Then
            firstBlankRow = currentRow + 1
        End If
    ElseIf (IsEmpty(currentRowValue) Or currentRowValue = "") Then
        If Not (IsEmpty(nextRowValue) Or nextRowValue = "") Then
            If firstBlankRow <> 0 Then
                lastBlankRow = currentRow
            End If
        End If
    End If
    Debug.Print "Row " & currentRow; ": firstBlankRow: " & firstBlankRow; ", lastBlankRow: " & lastBlankRow

'Group firstBlankRow & lastBlankRow
    'if first & last blank rows have been assigned, create a group
    If firstBlankRow <> 0 And lastBlankRow <> 0 Then
        'Debug.Print "Row: " & currentRow; ", Outline Level: " & ActiveSheet.Rows(currentRow).OutlineLevel
        If Not ActiveSheet.Rows(currentRow).OutlineLevel > 1 Then 'Ignore if last row is already grouped
            Range(Cells(firstBlankRow, myRange.Column), Cells(lastBlankRow, myRange.Column)).EntireRow.Select
            Selection.Group
        End If
        firstBlankRow = 0: lastBlankRow = 0 'reset the first/lastBlankRow values to 0
    End If
Next

Application.ScreenUpdating = True 'Start screen updating as macro is complete
End Sub
于 2016-06-20T01:31:29.183 回答