Merge Cells in Range

billandrew

Well-known Member
Joined
Mar 9, 2014
Messages
743
Looking to merge 2 cells in Range if there is data.

i.e.
Columns F:G

Columns I:J
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
This is the code I am using.
Dim rg as Range
Lr = cells(Rows.count,6).End(xlUp).Row
For i = 9 to Lr

Set rg = Range(Cells(i,6), Cells(I + 1, 7))

With rng

.HorizontalAlignment = True
.VerticalAlignment = True
.WrapText = True

End With

Next i
 
Upvote 0
Thank You

added the suggestion

My code looks like this below, Can this be cleaned up possibly?

Sub mergeColumns()
Dim rng1 As Range
Dim rng2 As Range
Dim rng3 As Range

lr = Cells(Rows.Count, 6).End(xlUp).Row

For i = 9 To lr

Set rng1 = Range(Cells(i, 6), Cells(i + 1, 7))

With rng1

.HorizontalAlignment = xlCenterAcrossSelection
.VerticalAlignment = xlCenter
.WrapText = True

End With

Next i

For i = 9 To lr

Set rng2 = Range(Cells(i, 9), Cells(i + 1, 10))

With rng2

.HorizontalAlignment = xlCenterAcrossSelection
.VerticalAlignment = xlCenter
.WrapText = True

End With

Next

For i = 9 To lr

Set rng2 = Range(Cells(i, 9), Cells(i + 1, 10))

With rng2

.HorizontalAlignment = xlCenterAcrossSelection
.VerticalAlignment = xlCenter
.WrapText = True

End With

Next



For i = 9 To lr

Set rng3 = Range(Cells(i, 11), Cells(i + 1, 13))

With rng3

.HorizontalAlignment = xlCenterAcrossSelection
.VerticalAlignment = xlCenter
.WrapText = True

End With

Next


Cells(1, 1).Select

End Sub
 
Upvote 0
Try this:
Code:
Sub mergeColumns()

    Dim rng1 As Range
    Dim rng2 As Range
    Dim rng3 As Range

    Application.ScreenUpdating = False

    lr = Cells(Rows.Count, 6).End(xlUp).Row

    For i = 9 To lr
        
        Set rng1 = Range(Cells(i, 6), Cells(i + 1, 7))
        With rng1
            .HorizontalAlignment = xlCenterAcrossSelection
            .VerticalAlignment = xlCenter
            .WrapText = True
        End With

        Set rng2 = Range(Cells(i, 9), Cells(i + 1, 10))
        With rng2
            .HorizontalAlignment = xlCenterAcrossSelection
            .VerticalAlignment = xlCenter
            .WrapText = True
        End With
    
        Set rng3 = Range(Cells(i, 11), Cells(i + 1, 13))
        With rng3
            .HorizontalAlignment = xlCenterAcrossSelection
            .VerticalAlignment = xlCenter
            .WrapText = True
        End With

    Next i

    Application.ScreenUpdating = True

    Cells(1, 1).Select

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,307
Members
452,633
Latest member
DougMo

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