How to unmerge cells while merging the data (if any) on the two cells

Shaan Bhasin

New Member
Joined
Jan 15, 2015
Messages
4
Hi,

I've cells on two rows which are merged, however, some cells are not merged and have different values/data. E.g. I want to bring Row 14-15 in one row also and delete the empty line and merge the comment section.
1695388124559.png


While I import the table in power query, it shows one row as nil value, so I want to cleanse the data before importing, Is there any smart solution either through VBA or power query, rather doing it manually?

Thanks in advance for your help.
 

Attachments

  • 1695381617125.png
    1695381617125.png
    5.7 KB · Views: 13

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Maybe using a macro. For example:
VBA Code:
Sub UnMerge()
'Ref: D:\DDownloads\[MULTI_C30923.xlsm]Foglio1
Dim SelAr As Range, nwTxt As String
Dim I As Long, J As Long, K As Long, mRC As Long
'
If Selection.Count < 5 Then
    MsgBox ("Select the area to be checked, then retry")
    Exit Sub
End If
ActiveSheet.Copy after:=ActiveSheet
Set SelAr = Selection
For I = SelAr.Rows.Count To 1 Step -1
    mRC = SelAr.Cells(I, 1).MergeArea.Rows.Count - 1
    If mRC > 0 Then
        For J = 1 To SelAr.Columns.Count
            If SelAr.Cells(I - mRC, J).MergeArea.Count > 1 Then
                SelAr.Cells(I - mRC, J).UnMerge
            Else
                nwTxt = ""
                For K = 0 To mRC
                    If SelAr.Cells(I - mRC + K, J) <> "" Then
                        nwTxt = nwTxt & " " & SelAr.Cells(I - mRC + K, J).Value
                    End If
                Next K
                Debug.Print SelAr.Cells(I - mRC, J).Resize(mRC + 1, 1).Address(0, 0), SelAr.Cells(I - mRC, J).Address(0, 0)
                SelAr.Cells(I - mRC, J).Resize(mRC + 1, 1).ClearContents
                SelAr.Cells(I - mRC, J).Value = Mid(nwTxt, 2)
            End If
        Next J
        Debug.Print SelAr.Cells(I - mRC + 1, J).Resize(mRC).Address(0, 0)
        SelAr.Cells(I - mRC + 1, J).Resize(mRC).EntireRow.Delete
        I = I - mRC
    End If
Next I
SelAr.Cells(1, 1).Select
MsgBox ("Completed, double-check the output")
End Sub
Copy the code into a vba Standard Module.
Then SELECT the area to rework and run Sub UnMerge:
-a copy of the original worksheet will be created, and the process to unmerge rows and merge text will be executed
-you need to check that the output is correct against the input

Be aware that the code doesn't deal with merged columns
See the image for the results of my test

Try...
 

Attachments

  • UNM1_Immagine 2023-09-23 010932.jpg
    UNM1_Immagine 2023-09-23 010932.jpg
    19.8 KB · Views: 10
  • UNM2_Immagine 2023-09-23 011018.jpg
    UNM2_Immagine 2023-09-23 011018.jpg
    18 KB · Views: 9
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,162
Members
453,021
Latest member
Justyna P

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