We help IT Professionals succeed at work.
Get Started

VBA to merge duplicated cells in selected range

Last Modified: 2012-05-12
I'm trying to come up with a macro that finds Table2 and merges identical cells.

To find Table2 in the attached sheet, I could use something like this:
Dim rFind As Range
    With Sheet1
        Set rFind = .Cells.Find(what:="Table2", lookat:=xlWhole, MatchCase:=False, SearchFormat:=False)
        If Not rFind Is Nothing Then
            .Cells(rFind.Row + 1, "A").Select
        End If
    End With

Open in new window

Below is another code that can be used to merge identical cells in column D thru AD (compliments of Nawaf).  If you see my attached spreadsheet, you'll see this range has nothing to do with my scenario, but the logic may give some food for thought.
Sub MergeCells() 
    Dim lRow As Long 
    lRow = Cells(Rows.Count, "D").End(xlUp).Row 
    Application.DisplayAlerts = False 
    For i = lRow To 2 Step -1 
        If Cells(i, 4) = Cells(i - 1, 4) Then 
            Range(Cells(i, 4), Cells(i - 1, 4)).Merge 
            Range(Cells(i, 33), Cells(i - 1, 33)).Merge 
        End If 
    Next i 
    Application.DisplayAlerts = True 
End Sub 

Open in new window

If an expert could give me some direction on how to bring this together and apply it to my scenario, I'd appreciate it.  Thanks!

Watch Question
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE