Excel vba - 在空白之间分组行

标签 excel vba grouping

请耐心等待,我在这里提出的第一个问题。

作为更大格式化过程的一部分,我正在尝试将原始数据提取中空白小计行之间的行分组为大纲。

示例数据看起来像(对不起,仍然是新用户,据我所知,不允许粘贴。):
Example Data
Example Result

一直在处理我在 Grouping Rows in VBA 中找到的答案

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

它将空白单元格组合在一起,但我需要弄清楚如何让 lastBlankRow 在下一个数据 block 之后拾取下一个空白行。

提前致谢!

最佳答案

您可以通过使用 Do While 以更少的代码实现此目的。嵌套在您的 For-Next 中的循环并在 B 列中保留非空白单元格的计数,使用该计数进行分组 - 1,然后将该计数添加到您的迭代中。

通过重新创建您的数据结构进行测试并取得了预期的结果:

Sub GroupRows()

    Dim r As Long
    Dim i As Long
    Dim rowCount As Long

    rowCount = Range("A" & Rows.Count).End(xlUp).Row

    For i = 2 To rowCount
        r = 0
        Do While Range("B" & i + r) <> "" And Not IsEmpty(Range("B" & i + r))
            r = r + 1
        Loop
        Range("B" & i & ":B" & i + r - 1).Rows.Group
        i = i + r
    Next i

End Sub

关于Excel vba - 在空白之间分组行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54467591/

相关文章:

python - 如何使用公共(public)列作为引用将数据从数据框的列复制到另一个数据框?

Excel 2016 向 LAST ROW 插入新表行弄乱了公式

vba - VBA中类初始化时的参数

r - 按 R 中的数字对数据框中的行进行分组和标签

r - 查找多个点之间的最短距离

excel - 循环十进制数列

arrays - 确定数组中的元素数

vba - 给定字符串查找单元格的列

excel - 检测单元格中的粗体文本并在这些文本前添加 ";"

python - Pandas - 在每日人口普查数据中查找唯一条目