how to merge cells containing same content

egmuni

New Member
Joined
Apr 26, 2019
Messages
1
Hi everyone, I have a dataset in which the top row gives me the days of the year and the cells below contain a formula giving me the name of a promo that covers a certain amount of days. The result is a dataset in which every row has a certain amount of contigous cells containing the same content. While Others remain blank. I want to merge and center the cells with the same content. I am using this code, which worked in the past for a slightly similar dataset, and it is not working now. Can anyone correct it?
Here it is the code:
sub merge_same_cells()
Application.DisplayAlerts=False
Dim rng As Range
MergeCells:
For Each rng In Selection
If rng.value=rng.Offset(1,0).value and rng.value<>"" then
Range(rng,rng.Offset(1,0)).merge
Range(rng.Offset(1,0)).HorizontalAlignment=xlcenter
Range(rng.Offset(1,0)).VerticalAlignment=xlcenter
GoTo MergeCells
End If
Next
End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".

Forum statistics

Threads
1,215,350
Messages
6,124,430
Members
449,158
Latest member
burk0007

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