0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

VBA 結合されたセルの解除と解除した全セルに値を設定

Last updated at Posted at 2020-02-14

全カラムのマージを解除

'全カラムのマージを解除
Private Sub UnMargeAllCell(ByRef wsWorkSheet As Worksheet)
    
    Dim rngRange                As Range
    Dim strMargeAreaAddress     As String
    
    For Each rngRange In wsWorkSheet.UsedRange
        If rngRange.MergeCells = True Then
            strMargeAreaAddress = rngRange.MergeArea.Address
            rngRange.MergeArea.UnMerge
            wsWorkSheet.Range(strMargeAreaAddress).Value = rngRange.MergeArea.Resize(1, 1).Value
        End If
    Next
    
End Sub
'範囲を指定して、セルの結合を解除(こっちの方がスピード的に現実的かな)
Private Sub UnMargeAllCell(ByRef wsWorkSheet As Worksheet)
    
    Dim strMargeAreaAddress     As String
    Dim lngLoopRow              As Long
    Dim lngLoopCol              As Long
    
    For lngLoopRow = 1 To 3
        For lngLoopCol = 1 To 200
            DoEvents
            Call dispInformationAtStatusBar("UnMerge " & lngLoopRow & "-" & lngLoopCol)
            If wsWorkSheet.Cells(lngLoopRow, lngLoopCol).MergeCells = True Then
                '解除前のアドレスを保持
                strMargeAreaAddress = wsWorkSheet.Cells(lngLoopRow, lngLoopCol).MergeArea.Address
                DoEvents
                'マージを解除
                wsWorkSheet.Cells(lngLoopRow, lngLoopCol).MergeArea.UnMerge
                DoEvents
                '解除したセルに値をコピー
                wsWorkSheet.Range(strMargeAreaAddress).Value = wsWorkSheet.Cells(lngLoopRow, lngLoopCol).MergeArea.Resize(1, 1).Value
            End If
        Next lngLoopCol
    Next lngLoopRow
    
End Sub

最終行と最終列

.Cells(.Rows.Count, 1).End(xlUp).Row
.Cells(1, .Columns.Count).End(xlToLeft).Column
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?