vba to merge cells

dshafique

Board Regular
Joined
Jun 19, 2017
Messages
144
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,664
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
144
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,664
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,082,360
Messages
5,364,927
Members
400,815
Latest member
Joaquin Phoenix

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top