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

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
38,150
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
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

tonyjyoo

Board Regular
Joined
Aug 5, 2016
Messages
167
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,191,587
Messages
5,987,506
Members
440,098
Latest member
MickyMouse123

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
Top