vba to merge cells

dshafique

Board Regular
Joined
Jun 19, 2017
Messages
150
Hey guys, im looking for a vba to merge the current cell with all the blank cells below it. I'm automating a report, which turns into a pivot table, and then i need that pivot table in text format for something else. I was thinking of doing something like this and merge cells while recording.

Code:
Range("A91").Select
    Range(Selection, Selection.End(xlDown)).Select
but this also selects the next line with text. the data looks kind of like

Shah, karan
Jones, Dave
Soni, Anand
Thomas, Melissa
Davis, Nathaniel
Kent, Michael
Lee, SabrinaPatel, Shruti
Charles, Brent

<tbody>
</tbody>

I want it so it merges with the cells below, the data is like a sideways tree i guess?
 

footoo

Well-known Member
Joined
Sep 21, 2016
Messages
2,755
Range(Selection, Selection.End(xlDown)(0)).Select

What do you want your data to look like after merging?
Does the data start in A2 columns A:C ?
 

dshafique

Board Regular
Joined
Jun 19, 2017
Messages
150
thats helpful in what im trying to do. I have pictures, but not sure if i can attach them, I'm trying to write a code which checks if the cell below is blank, if its blank, i want the current cell to merge with all the lank cells below, and iterate it through the table.

Code:
 Dim cl As Range
For Each cl In Range("A86:D153")
' going to replace the A86:D153 with a name for the table, since the data will be dynamic
        If IsEmpty(cl.Offset(1)) Then
          Range(Selection, Selection.End(xlDown)(0)).Select
            Selection.Merge
            With Selection
                .HorizontalAlignment = xlCenter
                .VerticalAlignment = xlBottom
                .WrapText = False
                .Orientation = 0
                .AddIndent = False
                .IndentLevel = 0
                .ShrinkToFit = False
                .ReadingOrder = xlContext
                .MergeCells = False
            End With
            With Selection
                .HorizontalAlignment = xlCenter
                .VerticalAlignment = xlCenter
                .WrapText = False
                .Orientation = 0
                .AddIndent = False
                .IndentLevel = 0
                .ShrinkToFit = False
                .ReadingOrder = xlContext
                .MergeCells = True
            End With
    
    Else: Selection.End(xlDown).Select
        End If
        Next cl
 

footoo

Well-known Member
Joined
Sep 21, 2016
Messages
2,755
Code:
Dim rng As Range, cl As Range, lr&, lc As Range
Set rng = Range("A86:D153")
lr = rng(rng.Cells.Count).Row
With rng
    .HorizontalAlignment = xlCenter
    .VerticalAlignment = xlCenter
    .WrapText = False
    .Orientation = 0
    .AddIndent = False
    .IndentLevel = 0
    .ShrinkToFit = False
    .ReadingOrder = xlContext
    .MergeCells = False
End With
'Set rng = rng.SpecialCells(xlCellTypeConstants)
For Each cl In rng
    If IsEmpty(cl.Offset(1)) Then
        Set lc = cl.End(xlDown)(0)
        If lc.Row > lr Then Set lc = Cells(lr, cl.Column)
        Range(cl, lc).Merge
    End If
Next cl
 
Last edited:

Forum statistics

Threads
1,086,235
Messages
5,388,623
Members
402,127
Latest member
Jemx

Some videos you may like

This Week's Hot Topics

Top