この投稿の年齢にもかかわらず、私はそれにつまずくかもしれない人のために私の2セントを投入すると思いました。私はあなたの質問を正しく理解することを望みます。これが私が集めたものです:
目標:対象の列のすべての行について、基準に基づいて行をグループ化します。
基準:rows in the group
値がない(空白、null、空)か、値があり、値が0の隣接セル(すぐ左)があるrows not in the group
ものだけです。空白ではなく、 0ではない隣接セル。
サンプルデータは次のとおりです。
注:範囲は、OPが言っているようにB1:B12
、名前付き範囲を構成します。rngList
マクロを実行する前のデータ:

マクロ実行後のデータ-折りたたまれていないグループ化:

マクロ実行後のデータ-グループ化は折りたたまれています:

これを処理するコード:
このコードを機能させるには:VBE(Visual Basic Editor)で、グループ化するデータ(名前付き範囲も含む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