EXCELのグループ化についてお伺いいたします。次のコードで、連続しない空白セルをグループ化したいです。間違っている箇所を教えてください。どうぞよろしくお願い致します。Sub GroupByDate() Dim lastRow As Long Dim i As Long, startDateRow As Long, endDateRow As Long Dim blankRows As Collection Dim r As Long, c As Long Dim isBlankAll As Boolean lastRow = Cells(Rows.Count, \u0026quot;A\u0026quot;).End(xlUp).Row i = 1 Do While i \u0026lt;= lastRow ' --- A列結合(日付ブロック)範囲取得 --- If Cells(i, \u0026quot;A\u0026quot;).MergeCells Then startDateRow = Cells(i, \u0026quot;A\u0026quot;).MergeArea.Row endDateRow = startDateRow + Cells(i, \u0026quot;A\u0026quot;).MergeArea.Rows.Count - 1 Else startDateRow = i endDateRow = i End If ' --- 完全空白行だけ収集 --- Set blankRows = New Collection For r = startDateRow To endDateRow ' A〜Eすべて空白か判定 isBlankAll = True For c = 1 To 5 ' A=1〜E=5 If Trim(Cells(r, c).Value) \u0026lt;\u0026gt; \u0026quot;\u0026quot; Then isBlankAll = False Exit For End If Next c If isBlankAll Then blankRows.Add r End If Next r ' --- グループ化(完全空白行のみ) --- If blankRows.Count \u0026gt; 0 Then Rows(blankRows(1) \u0026amp; \u0026quot;:\u0026quot; \u0026amp; blankRows(blankRows.Count)).Group End If ' 次のブロックへ i = endDateRow + 1 LoopEnd Sub

Excel

1件の回答

回答を書く

1270018

2026-06-17 08:45

+ フォロー

間違っているのではなく条件を追加すればいいだけなんですが、

少しコードが冗長な感じがします。



解釈が合っているかどうか?





Sub test()

Dim i As Long

Columns(1).ClearOutline

For i = 2 To Cells(Rows.Count, 1).End(xlUp).Row

If (Cells(i, 1).MergeCells And i = Cells(i, 1).MergeArea.Cells(1, 1).Row) Or _

(Cells(i - 1, 1) \u0026lt;\u0026gt; \u0026quot;\u0026quot; And Cells(i + 1, 1) \u0026lt;\u0026gt; \u0026quot;\u0026quot; And Cells(i, 1) = \u0026quot;\u0026quot;) Then

Cells(i, 1).MergeArea.Rows.Group

End If

Next

End Sub

うったえる有益だ(0シェアするブックマークする

関連質問

Copyright © 2026 AQ188.com All Rights Reserved.

博識 著作権所有