Merge cells based on duplicate values in adjacent cells

sabrez

New Member
Joined
Oct 25, 2017
Messages
12
Hi

I have a table as below:

1$20
1$20
2$30
3$15
3$15

<tbody>
</tbody>

Now, I want to merge the cells in the 2nd column for corresponding duplicate values in col 1, as below:

1
1$20
2$30
3
3$15

<tbody>
</tbody>


Regards
Sabrez
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Hi,



Ctrl+Shift+Enter NOT just Enter

E2=IFERROR(IFERROR(INDEX($B$2:$B$6, MATCH(0, COUNTIF($E$1:E1,$B$2:$B$6), 0)),INDEX($B$2:$B$6, MATCH(0, COUNTIF($E$1:E1,$B$2:$B$6),0))), "")

Just Enter

D2 =INDEX($A$2:$A$6,MATCH(E2,$B$2:$B$6,0))


ABCDE
11$201$20
21$202$30
32$303$15
43$15
53$15

<colgroup><col width="70" span="6" style="width:52pt"> </colgroup><tbody>
</tbody>
 
Upvote 0
Hi sabrez,

from what I understand, you want to merge the cells in the second column based on common value in column 1. The code below assumes your data starts in A1 and doesn't matter if your data has headings or not.

Sub Macro1()

Dim lc As Integer
Dim x As Integer
Dim i As Integer
Dim d As Range

Application.DisplayAlerts = False

Set d = ThisWorkbook.Sheets(1).UsedRange
lc = d.Columns.Count

i = 1
For Each cell In d.Columns(1).Cells

If cell.Value = cell.Offset(1, 0).Value And x = 0 Then
x = cell.Row
i = i + 1

ElseIf Application.WorksheetFunction.CountIf(ThisWorkbook.Sheets(1).Range("A1:A" & i), cell.Value) <> 1 And cell.Value <> cell.Offset(1, 0).Value Then

Range(Cells(x, lc), cell.Offset(0, 1)).Select
Selection.Merge
x = 0
i = i + 1

Else

i = i + 1

End If

Next cell

Application.DisplayAlerts = True

End Sub
 
Upvote 0
Maybe this is better, assuming duplicated values is in column 1 and values to merge is in the column next to column 1.

Sub Macro1()

Dim lc As Integer
Dim x As Integer
Dim d As Range

Application.DisplayAlerts = False

Set d = ThisWorkbook.Sheets(1).UsedRange
lc = d.Columns.Count

For Each cell In d.Columns(1).Cells

If cell.Value = cell.Offset(1, 0).Value And x = 0 Then
x = cell.Row

ElseIf Application.WorksheetFunction.CountIf(d.Columns(1), cell.Value) <> 1 And cell.Value <> cell.Offset(1, 0).Value Then

Range(cell.Offset(-(cell.Row - x), 1), cell.Offset(0, 1)).Select
Selection.Merge
x = 0

End If

Next cell

Application.DisplayAlerts = True

End Sub
 
Upvote 0
Maybe this is better, assuming duplicated values is in column 1 and values to merge is in the column next to column 1.

Sub Macro1()

Dim lc As Integer
Dim x As Integer
Dim d As Range

Application.DisplayAlerts = False

Set d = ThisWorkbook.Sheets(1).UsedRange
lc = d.Columns.Count

For Each cell In d.Columns(1).Cells

If cell.Value = cell.Offset(1, 0).Value And x = 0 Then
x = cell.Row

ElseIf Application.WorksheetFunction.CountIf(d.Columns(1), cell.Value) <> 1 And cell.Value <> cell.Offset(1, 0).Value Then

Range(cell.Offset(-(cell.Row - x), 1), cell.Offset(0, 1)).Select
Selection.Merge
x = 0

End If

Next cell

Application.DisplayAlerts = True

End Sub


Perfect!! This is exactly what I wanted. Thanks a ton

Regards
Sabrez
 
Upvote 0

Forum statistics

Threads
1,216,212
Messages
6,129,540
Members
449,515
Latest member
lukaderanged

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