Merge and Center one Row

datauser

New Member
Joined
Sep 4, 2024
Messages
23
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

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
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,224,800
Messages
6,181,045
Members
453,014
Latest member
Chris258

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