Need help merging every other row via VBA

otimo

New Member
Joined
Mar 24, 2020
Messages
3
Office Version
  1. 2016
Platform
  1. Windows
I have tried using the code below to merge every other row (i.e. A2 and A3, A4 and A5, A6 and A7, etc.), however I am having an issue doing so as the following happens (i.e. A2 and A4, A5 and A7, A8 and A10, etc.). Instead of every two (2) rows being merged it seems as though three (3) are being merged instead. Any help to the following please:

Sub MacroTomerge()

Application.DisplayAlerts = False
Application.ScreenUpdating = False

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

End Sub
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Does this work for you


VBA Code:
   Application.DisplayAlerts = False

   For i = 1 To Range("A" & Rows.Count).End(xlUp).Row Step 2

        With Range("A" & i).Resize(2, 1)

            .MergeCells = True

            .HorizontalAlignment = xlCenter

            .VerticalAlignment = xlCenter

        End With

    Next i

    Application.DisplayAlerts = True
 
Upvote 0
Yes it works perfectly. I realized that I was using Step 3 instead of Step 2. Thanks a lot. :)
 
Upvote 0

Forum statistics

Threads
1,215,788
Messages
6,126,907
Members
449,348
Latest member
Rdeane

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