Merge cells with formula or macro?

J

Joe M.

I would like to merge cells in column A when the adjacent vertical cells are
the same. For example:

Col A
Row1 CX457
Row2 AG925
Row3 AG925
Row4 AG925
Row5 RB639
Row6 RB639

In this case A2, A3 & A4 would be merged, A5 & A6 would be merged. Is there
a way to do this automatically?

Thanks,
Joe M.
 
B

Barb Reinhardt

I'd recommend against merging cells. Why not use a conditional format and if
the cells match, have one of them change to a white font?
 
D

Dave O

Merged? As in concatenated (added together to read RB639 RB639 in a
single cell)?

Or do you want to show only a single occurence if the entry occurs
multiple times?
 
B

bj

what do you mean by merging cells?
merging can mean anything from
summing up values.
Deleting duplicates,
to formatting several cells to appear as one cell.
 
J

Joe M.

Sorry for not being clear. In this case, merge as to format several cells to
appear as one cell.
 
B

bj

I have had a lotr of trouble with merged cells over the years, but a brute
force macro to do what you want would be

Sub test()
rt = 1
For r = 2 To 50000
If Cells(r, 1) = "" Then GoTo 99
If Cells(r - 1, 1) <> Cells(r, 1) Then rt = r: GoTo 99
If Cells(r - 1, 1) = Cells(r + 1, 1) Then GoTo 99
If Cells(r - 1) = Cells(r, 1) Then
Range(Cells(rt, 1), Cells(r, 1)).Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
End If
99
Next r

change the formating of the merged cells as you would like them to appear

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top