loop to merge cells

tonyjyoo

Board Regular
Joined
Aug 5, 2016
Messages
167
I need a VBA that goes down 1 single column, merges with blank cells (rows may vary), then loops until finished.

For example:

A
B
C
D

<tbody>
</tbody>

A would need to be grouped with the blank cell below, same with B, C would need to be merged with the 3 blank cells below, etc.

Any help appreciated.

Thanks.
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Assuming the data you want to merge is in Column A, give this macro a try (note that it calculates the last data row from among all your columns and uses that to set the merge range for the last data item in Column A)...
Code:
Sub MergeCellsInColumnA()
  Dim LastRow As Long, Ar As Range
  LastRow = Cells.Find("*", , xlValues, , xlRows, xlPrevious).Row
  For Each Ar In Range("A1:A" & LastRow).SpecialCells(xlBlanks).Areas
    Ar(1).Offset(-1).Resize(Ar.Count + 1).Merge
  Next
End Sub
 
Last edited:
Upvote 0
Assuming the data you want to merge is in Column A, give this macro a try (note that it calculates the last data row from among all your columns and uses that to set the merge range for the last data item in Column A)...
Code:
Sub MergeCellsInColumnA()
  Dim LastRow As Long, Ar As Range
  LastRow = Cells.Find("*", , xlValues, , xlRows, xlPrevious).Row
  For Each Ar In Range("A1:A" & LastRow).SpecialCells(xlBlanks).Areas
    Ar(1).Offset(-1).Resize(Ar.Count + 1).Merge
  Next
End Sub

Perfect.Thank you as always Rick!
 
Upvote 0

Forum statistics

Threads
1,214,644
Messages
6,120,709
Members
448,983
Latest member
Joaquim_Baptista

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