Merge cells by count vba

donniedorian

Board Regular
Joined
May 27, 2015
Messages
133
Hello,

I need a macro to merge as many cell in B as cells the same in a column A

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD][/TD]
[TD]A
[/TD]
[TD]B
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]1
[/TD]
[TD]aaaaa
[/TD]
[TD]merg 4
[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]2
[/TD]
[TD]aaaaa
[/TD]
[TD]merg 4[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]3
[/TD]
[TD]aaaaa
[/TD]
[TD]merg 4
[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]aaaaa
[/TD]
[TD]merg 4[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]5
[/TD]
[TD]bb
[/TD]
[TD]merge 2
[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]6
[/TD]
[TD]bb
[/TD]
[TD]merge 2
[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]7
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
I don't see which cells are merged in col A in your post.
But try like this:

Code:
Sub MergeBAsInColA()
Dim cellsA As Range
For Each cellsA In Sheets("Sheet1").Range("A1:A50")
    If cellsA.MergeCells Then
        Range(Replace(cellsA.MergeArea.Address, "A", "B")).Merge
    End If
Next cellsA
End Sub
 
Upvote 0
Hi Kokosek,
I need as many cells to be merged in column B as cells with the same value in column A
column A has 4 rows of the same cells aaaa so I need 4 rows to be merged adjacent in column B
for the bbb value I need two cells in column B to me merged
 
Upvote 0
That's definitely more clear for me.


Code:
Sub MergeBbasedonValinA()
Dim cellsA As Range
Dim i As Long
i = 1
For Each cellsA In Sheets("Sheet1").Range("A2:A50") 'expand range to your needs
    If cellsA.Value <> cellsA.Offset(1, 0).Value Then
        Range("B" & i & ":B" & cellsA.Row).Merge
        i = cellsA.Row + 1
    End If
Next cellsA


End Sub
 
Last edited:
Upvote 0
Hi all,

Does anyone have an idea how to adapt this code to the number of items of a listbox ?

Here is the code of 2 istboxes from a userform where all the items displayed in listbox 1 and listbox 2 are written in the column B of an Excel sheet

Code:
Sheet9.Activate


For b = 0 To ListBox1.ListCount - 1
With Sheets("Data")
     nextRow = .Range("B" & Rows.Count).End(xlUp).Row + 1
     .Range("B" & nextRow) = ListBox1.List(b)
End With
Next


For c = 0 To ListBox2.ListCount - 1
With Sheets("Data")
     nextRow = .Range("B" & Rows.Count).End(xlUp).Row + 1
     .Range("B" & nextRow) = ListBox2.List(c)
End With
Next

But how to merge the cells in the column A where the number of cells of the column A represent the number of items of the listbox 1 + the listbox 2 ?

Any ideas ?:confused:
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,209
Members
453,023
Latest member
alabaz

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