1

I would like to merge two cells as one and I have to do that for 2000+ rows (1000+) if merged. I am looking for a macro that will help with this. Below is an example of what I would like to do..

I have used the basic macro recorder and its a lot of cells that I have hard code, I have 2003 rows that I need to do the below too.

Sub Macro2()
'
' Macro2 Macro
'

'
    Range("A28:A29,C28:C29,E28:E29,F28:F29").Select
    Range("F28").Activate
    With Selection
        .VerticalAlignment = xlCenter
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = True
    End With
    Range("A1").Select
End Sub

Below is an example of the data that I want to merge... https://i.sstatic.net/US0MG.jpg

Number  Def Name1   Name2   Group1  Group2
12345   1   abcd             1       2
12345   2   abcd             1       2
123456  1   abcde            5       8
123456  2   abcde            5       8
123789  1   qwert            2       5
123789  2   qwert            2       5

After merging , I would like to see the below: https://i.sstatic.net/Pz0tb.jpg

Number  Def Name1   Name2   Group1  Group2
12345    1  abcd                 1       2
         2              
123456   1  abcde                5       8
         2              
123789   1  qwert                2       5
         2              

Thanks for your help in this matter!

Regards, Samit

1 Answer 1

1
Sub mergerizer()

Application.DisplayAlerts = False

Dim r As Integer
Dim mRng As Range
Dim rngArray(1 To 4) As Range
r = Range("A65536").End(xlUp).Row

For myRow = r To 2 Step -1

    If Range("A" & myRow).Value = Range("A" & (myRow - 1)).Value Then

        For cRow = (myRow - 1) To 1 Step -1

            If Range("A" & myRow).Value <> Range("A" & cRow).Value Then

                Set rngArray(1) = Range("A" & myRow & ":A" & (cRow + 1))
                Set rngArray(2) = Range("C" & myRow & ":C" & (cRow + 1))
                Set rngArray(3) = Range("E" & myRow & ":E" & (cRow + 1))
                Set rngArray(4) = Range("F" & myRow & ":F" & (cRow + 1))

                For i = 1 To 4
                    Set mRng = rngArray(i)
                    mRng.Merge
                    With mRng
                        .HorizontalAlignment = xlCenter
                        .VerticalAlignment = xlTop
                        .WrapText = False
                        .Orientation = 0
                        .AddIndent = False
                        .IndentLevel = 0
                        .ShrinkToFit = False
                        .ReadingOrder = xlContext
                        .MergeCells = True
                    End With

                Next i

                myRow = cRow + 1
                Exit For
            End If
        Next cRow
    End If
Next myRow

Application.DisplayAlerts = True

End Sub

its not crazy elegant but i tested it and it works :) good luck

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much Mr. Monshaw! that did the trick and saved me a lot of time.
my pleasure, pass the help along :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.