Merge Cells in a column Macro

M

Mike G.

Experts & Gurus:

I need to merge cells in a column that have exactly the
same information. For example, cells E2, E3, and E4 have
the same information.

Existing spreadsheet:
A B C D E F
1 aa ss dd ff gg hh
2 qq ww ee rr tt yy
3 cc vv bb nn tt mm
4 hh uu ii oo tt kk

Desired speadsheet:
A B C D E F
1 aa ss dd ff gg hh
2 qq ww ee rr yy
3 cc vv bb nn tt mm
4 hh uu ii oo kk

I will sort column E, then I want the macro to look in
column E and if 2 or more cells are the same I want the
cells to merge.

Your help is appreciated!
Best regards, Mike
mailto:[email protected]
 
F

Frank Kabel

Hi
try the following macro for column E:
Sub merge_cells()
Dim lastrow As Long
Dim row_index As Long
Dim start_index As Long
Dim wks As Worksheet

Set wks = ActiveSheet
With wks
lastrow = .Cells(Rows.Count, "E").End(xlUp).Row
start_index = 1
For row_index = 2 To lastrow + 1
If .Cells(row_index, 5).Value <> .Cells(row_index - 1, 5).Value
Then
If row_index - start_index > 1 Then
Application.DisplayAlerts = False
With Range(.Cells(start_index, 5), .Cells(row_index - 1, 5))
.Merge
.VerticalAlignment = xlCenter
End With
Application.DisplayAlerts = True
End If
start_index = row_index
End If
Next
End With
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