Use formula or VBA to merge cells

woah

New Member
Joined
May 10, 2023
Messages
24
Office Version
  1. 2016
Platform
  1. Windows
Hi there,

I got data like the picture. I want to merge cells of the raw data, but some contains dpulicate cells and blank cells. How can i use formula or VBA to have the desired outcome like the picture?

I got several sets of data. Some cells just contains values, but some cells contatin formula. Not sure whether it will affect the outcome.

Thank you all for your advice.

P.S. I have installed XL2BB, but nothing happened when I tried to paste it in the post. Not sure how to be able to use it.

1684658260779.png
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Hi
Try
VBA Code:
Sub test()
Dim lr&, i&
lr = Cells(Rows.Count, 3).End(xlUp).Row
With Range("B3:B" & lr)
    .SpecialCells(4).FormulaR1C1 = "=R[-1]C"
    .Value = .Value
 Application.DisplayAlerts = False
    For i = lr To 3 Step -1
        If Cells(i, 2) = Cells(i + 1, 2) Then Range(Cells(i, 2), Cells(i + 1, 2)).Merge
    Next
 .HorizontalAlignment = xlCenter
 .VerticalAlignment = xlCenter
  End With
Application.DisplayAlerts = True
End Sub
 
Upvote 1
Solution
You are very welcome
And thank you for the feedback
Be happy and safe
 
Upvote 1

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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