Home > Back-end >  Excel VBA to merge every other row
Excel VBA to merge every other row

Time:11-12

I need to merge cells in every other row in a worksheet, so it would be rows 3, 5, 7, 9 etc for however many rows there are in the data, for columns B to L. So far I have been performing this task manually as I have not been able to replicate the process using VBA and could not find a solution online - I'd be very grateful if someone could please help with the VBA - the code below is fairly close to what I need but it merges cells A2 with A3 then B2 with B3 etc - I would need it to merge cells B3 to L3 then B5 to L5, B7 to L7 etc. Kindest regards and thanks in advance. TE

Sub Merge_alternate_rows()
Application.DisplayAlerts = False
Application.ScreenUpdating = False
For i = 2 To Range("A" & Rows.Count).End(xlUp).Row Step 3
    For j = 1 To Columns("L").Column
        With Range(Cells(i, j), Cells(i   1, j))
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlCenter
            .MergeCells = True
        End With
    Next
Next
End Sub

CodePudding user response:

Sub Merge_alternate_rows()
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    For i = 3 To Range("A" & Rows.Count).End(xlUp).Row Step 2
        'For j = 1 To Columns("L").Column
            With Range(Cells(i, 2), Cells(i, 12))
                .HorizontalAlignment = xlCenter
                .VerticalAlignment = xlCenter
                .MergeCells = True
            End With
        Next
    'Next
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
End Sub
  • Related