Merge Duplicate Cells in Column Relative to Data in Next Column

FrenchCelt

Board Regular
Joined
May 22, 2018
Messages
214
Office Version
  1. 365
Platform
  1. Windows
Hello,

I'm trying to add some VBA code to a formatting macro that will take duplicate data down Column A (the data starts in A4) and merge them to fit the size of the merged cells in Column B. So something like this:
Screenshot 2023-02-20 073006.png

To something like this:
Screenshot 2023-02-20 073210.png

In looking around on my own, I have only seen code that can combine all the duplicates into one grand merged cell, and that's not what I want. Can anyone help?
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
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 Or rng.Value <>"" Then
Range(rng, rng.Offset(1, 0)).Merge
Range(rng, rng.Offset(1, 0)).HorizontalAlignment = xlCenter
Range(rng, rng.Offset(1, 0)).VerticalAlignment = xlCenter
GoTo MergeCells
End If
Next
 
Upvote 0
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, rng.Offset(1, 0)).HorizontalAlignment = xlCenter
Range(rng, rng.Offset(1, 0)).VerticalAlignment = xlCenter
GoTo MergeCells
End If
Next
 
Upvote 0
Thanks for the reply. Since it had been so long since my original post, I worked out a solution via other means. I did give your code a try since it looked a little shorter, and I prefer code elegance, but it had no effect. Thanks anyway.

Incidentally, here is the code that worked for me. I had to take some extra steps in the code to put the data in a way that would work by eliminating all the duplicate shift info per person (I used XLOOKUP to replace the shift data generated by the report by looking it up from another database and then did find and replace to remove the #N/A results generated in all the cells where there wasn't a corresponding user ID in Column B).


VBA Code:
    Dim k, r   As Long
    Dim lr As Long: lr = Cells.Find("*", Cells(1, 1), xlFormulas, xlWhole, xlByRows, xlPrevious).Row
        For k = lr To 1 Step -1
        If Range("A" & k).Value <> "" And Range("A" & k).Offset(0, 1).MergeCells = True Then
        r = Range("A" & k).Offset(0, 1).MergeArea.Count
        Range("A" & k).Resize(r, 1).Merge
        Range("A" & k).VerticalAlignment = xlTop
        Range("A" & k).HorizontalAlignment = xlCenter
        End If
    Next k
 
Upvote 0
Solution

Forum statistics

Threads
1,214,940
Messages
6,122,352
Members
449,080
Latest member
Armadillos

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