VBA code to Delete unmerge cells and keep data

Kurt

Well-known Member
Joined
Jul 23, 2002
Messages
1,664
I have found the following code which does the first part perfectly

Code:
Sub FindMerged2()
'http://www.extendoffice.com/documents/excel/962-excel-select-merged-cells.html
Dim c As Range
    For Each c In ActiveSheet.UsedRange
    If c.MergeCells Then
        c.Interior.ColorIndex = 36
    End If
Next
End Sub
Sub FindMerged4()
Dim c As Range
Dim sMsg As String
sMsg = ""
    For Each c In ActiveSheet.UsedRange
        If c.MergeCells Then
            If sMsg = "" Then
                sMsg = "Merged worksheet cells:" & vbCr
            End If
                sMsg = sMsg & Replace(c.Address, "$", "") & vbCr
        End If
Next
        If sMsg = "" Then
            sMsg = "No merged worksheet cells."
        End If
MsgBox sMsg
End Sub

Now what is the best way to use the unmerge command in vba to make the merged cells in column A one cell and keep the rest of the data?
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
if you have a sample file or data that would help.

dunno usually when you Unmerge cells in VBA the data is retained, it does not go anywhere.

am saying this because i was solving something for a dude here in the forum this morning he had a problem with UnMergin some merged cells and changing their content. find the file below

http://www.filedropper.com/searchreplaceandunmerge
 
Last edited:
Upvote 0
Many thanks and hats off to Smitty for providing the answer.

Be sure to change the range to your particular needs:

Code:
Sub Get_Merged_Cells_Delete_Blanks2()
    Dim lr As Long
   
    Sheets("Sheet1").Copy After:=Sheets(3)
   
    With ActiveSheet
        lr = Cells(Rows.Count, "A").End(xlUp).Row
        With Range("$A$2:$Y$" & lr)
            .UnMerge
            .AutoFilter
            .AutoFilter Field:=1, Criteria1:="="
            .SpecialCells(xlCellTypeVisible).EntireRow.Delete
            .AutoFilter
        End With
    End With
   
End Sub

Awesome quick answer Smitty! :-)
 
Upvote 0
Many thanks and hats off to Smitty for providing the answer.

Be sure to change the range to your particular needs:

Code:
Sub Get_Merged_Cells_Delete_Blanks2()
    Dim lr As Long
   
    Sheets("Sheet1").Copy After:=Sheets(3)
   
    With ActiveSheet
        lr = Cells(Rows.Count, "A").End(xlUp).Row
        With Range("$A$2:$Y$" & lr)
            .UnMerge
            .AutoFilter
            .AutoFilter Field:=1, Criteria1:="="
            .SpecialCells(xlCellTypeVisible).EntireRow.Delete
            .AutoFilter
        End With
    End With
   
End Sub

Awesome quick answer Smitty! :-)

Use this code to make sure the Auto Filter is turned off:

Code:
Sub Get_Merged_Cells_Delete_Blanks2()
    Dim lr As Long
   
    Sheets("Sheet1").Copy After:=Sheets(3)
   
    With ActiveSheet
        lr = Cells(Rows.Count, "A").End(xlUp).Row
        With Range("$A$2:$Y$" & lr)
            .UnMerge
            .AutoFilter
            .AutoFilter Field:=1, Criteria1:="="
            .SpecialCells(xlCellTypeVisible).EntireRow.Delete
        End With
    End With
    If ActiveSheet.FilterMode = True Then
        Selection.AutoFilter
    End If
End Sub

Again many thanks Smitty! :-)
 
Upvote 0

Forum statistics

Threads
1,223,912
Messages
6,175,341
Members
452,638
Latest member
Oluwabukunmi

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top