【问题标题】:Excel vba - Group rows between blanksExcel vba - 在空白之间分组行
【发布时间】:2019-02-01 15:08:45
【问题描述】:

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

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

示例数据看起来像(抱歉,仍然是新用户,据我所知,不允许粘贴。): 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 在下一个数据块之后拾取下一个空白行。

提前致谢!

【问题讨论】:

  • 欢迎来到 SO。您能否提供一个示例或屏幕截图,说明正在发生的不希望发生的事情以及您希望发生的事情?很难想象这一切。
  • 另外,您可能需要考虑使用Long 而不是Integer。我不知道你是否会超过 32000 行,但这总是有风险的。另外,使用Long 是更好的做法,即使它使用更多内存。 stackoverflow.com/questions/26409117/…
  • 感谢反馈,希望示例截图对您有所帮助。

标签: excel vba grouping


【解决方案1】:

您可以通过使用嵌套在 For-Next 中的 Do While 循环并在 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

【讨论】:

  • 谢谢,Tate,正是我想要的,而且更简单!
  • 很高兴有帮助!