Merge and Center one Row

datauser

New Member
Joined
Sep 4, 2024
Messages
15
Office Version
  1. 365
Platform
  1. Windows
Hello!

I have created this macro that will merge and center. This macro does work, but merges and centers the entire row even when the cell is blank.

I would like the merge and center to process to stop at the last used cell in row 3.

Sub merge()
ncolumns = Sheets("Week to Week").Cells(4, Columns.Count).End(xlToLeft).Column

Dim RgToMerge As Range

For i = 3 To ActiveSheet.Cells(Rows.Count, 3).End(xlToLeft).Row Step 2

Set RgToMerge = Range(Cells(3, i), Cells(3, i + 1))
With RgToMerge
.merge
.HorizontalAlignment = xlCenterAcrossSelection
.VerticalAlignment = xlCenter
End With

Next i

End Sub

Thank You for any assistance!
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
How about
VBA Code:
For i = 3 To ActiveSheet.Cells(3, columns.count).End(xlToLeft).column Step 2

PS. What is ncolumns (calculated in first row used for? Is it some public variable aclculated in this procedure? or just a reminder of old code?
 
Upvote 0
Solution
This works! I removed the ncolumns and replaced my line of code with yours.
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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