Merging Empty Cells when Last Cell in Column&Row is Empty

helpexcel

Well-known Member
Joined
Oct 21, 2009
Messages
656
This code works great until the last column is blank. So the last column in row 22 is blank and should be merged with column 21. Colum 22 has data, but row 22 doesn't. It's reading the last column as the end of the spreadsheet. I've tried to play around with the changing the last column calculation to a different row, but no luck. Thanks for the help!

VBA Code:
Dim c3 As Long, c4 As Long
    c3 = 3
    Do
        If .Cells(22, c3 + 1) = "" Then
            c4 = .Cells(22, c3).End(xlToRight).Column
            If c4 < Columns.Count Then
                .Range(.Cells(22, c3), .Cells(31, c4 - 1)).HorizontalAlignment = xlCenterAcrossSelection
                c3 = c4
            Else
                Exit Do
            End If
        Else
            c3 = c3 + 1
        End If
    Loop
End With
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
got rid of the Do/Loop and used For/Next with range defined by lc2 and it works.

VBA Code:
Dim c3 As Long, c4 As Long, lc2 As Long, lc3 As Long
lc2 = .Cells(12, Columns.Count).End(xlToLeft).Column
lc3 = Columns.Count
 
 c3 = 3
For M = 3 To lc2
        If .Cells(22, c3 + 1) = "" Then
            c4 = .Cells(22, c3).End(xlToRight).Column
            If c4 < Columns.Count Then
                .Range(.Cells(22, c3), .Cells(31, c4 - 1)).HorizontalAlignment = xlCenterAcrossSelection
                c3 = c4
            ElseIf c4 = lc3 Then
                .Range(.Cells(22, c3), .Cells(31, (lc3 - (lc3 - lc2)))).HorizontalAlignment = xlCenterAcrossSelection
                c3 = c4
            End If
        Else
            c3 = c3 + 1
        End If
    M = M + 1
Next
 
Upvote 0
Solution

Forum statistics

Threads
1,215,034
Messages
6,122,782
Members
449,095
Latest member
m_smith_solihull

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