Sub Test5()
Dim c As Long, r As Long
Dim LevelName As String, pLevelName As String
Dim 始 As Long, 終 As Long
Application.ScreenUpdating = False
For c = 1 To 6 '1列から最終列の6列までループ
始 = 2
LevelName = Cells(2, c).MergeArea.Item(1).Value
If c = 1 Then
For r = 2 To 23 '2行~最終行の23行までループ
If Cells(r, c).MergeArea.Item(1).Value <> LevelName Then
終 = r - 1
LevelName = Cells(r, c).MergeArea.Item(1).Value
Application.DisplayAlerts = False
Range(Cells(始, c), Cells(終, c)).Merge
Application.DisplayAlerts = True
始 = r
ElseIf r = 23 Then '※最終行の数字
終 = r
Application.DisplayAlerts = False
Range(Cells(始, c), Cells(終, c)).Merge
Application.DisplayAlerts = True
始 = 2
End If
Next
Else
始 = 2
LevelName = Cells(2, c).MergeArea.Item(1).Value
pLevelName = Cells(2, c - 1).MergeArea.Item(1).Value
For r = 2 To 23
If Cells(r, c).MergeArea.Item(1).Value <> LevelName And _
Cells(r, c - 1).MergeArea.Item(1).Value = pLevelName Then
終 = r - 1
Application.DisplayAlerts = False
Range(Cells(始, c), Cells(終, c)).Merge
Application.DisplayAlerts = True
pLevelName = Cells(r, c - 1).MergeArea.Item(1).Value
LevelName = Cells(r, c).MergeArea.Item(1).Value
始 = r
ElseIf r = 23 And (Cells(r, c).MergeArea.Item(1).Value <> LevelName Or _
Cells(r, c - 1).MergeArea.Item(1).Value <> pLevelName) Then
終 = r - 1
Application.DisplayAlerts = False
Range(Cells(始, c), Cells(終, c)).Merge
Application.DisplayAlerts = True
始 = 2
ElseIf r = 23 Then '※最終行の数字
終 = r
Application.DisplayAlerts = False
Range(Cells(始, c), Cells(終, c)).Merge
Application.DisplayAlerts = True
始 = 2
ElseIf Cells(r, c).MergeArea.Item(1).Value <> LevelName And _
Cells(r, c - 1).MergeArea.Item(1).Value <> pLevelName Then
終 = r - 1
Application.DisplayAlerts = False
Range(Cells(始, c), Cells(終, c)).Merge
Application.DisplayAlerts = True
始 = r
pLevelName = Cells(r, c - 1).MergeArea.Item(1).Value
LevelName = Cells(r, c).MergeArea.Item(1).Value
ElseIf Cells(r, c).MergeArea.Item(1).Value = LevelName And _
Cells(r, c - 1).MergeArea.Item(1).Value <> pLevelName Then
終 = r - 1
Application.DisplayAlerts = False
Range(Cells(始, c), Cells(終, c)).Merge
Application.DisplayAlerts = True
始 = r
pLevelName = Cells(r, c - 1).MergeArea.Item(1).Value
LevelName = Cells(r, c).MergeArea.Item(1).Value
End If
Next
End If
Next
Application.ScreenUpdating = True
End Sub
お礼
ありがとうございます。 結果は変わりませんでした。 マックはともかく、ウィンで問題なく動作できましたので、これにで終了させてください。