Help merging like rows into one row

trey2008

New Member
Joined
Dec 22, 2017
Messages
5
I need some help writing a macro that will combine duplicate entries and delete out one of the entries. Please see table below: In below table I would like to combine the two entries for 123 on one row displaying all three reasons. I have a lot of data, so it would need to be a macro. Any help is appreciated

[TABLE="class: cms_table, width: 500"]
<tbody>[TR]
[TD]Request
[/TD]
[TD]Reason1
[/TD]
[TD]Reason2
[/TD]
[TD]Reason3
[/TD]
[/TR]
[TR]
[TD]123
[/TD]
[TD]a
[/TD]
[TD]b
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]123
[/TD]
[TD]c
[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]564
[/TD]
[TD]a
[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]432
[/TD]
[TD]a
[/TD]
[TD]b
[/TD]
[/TR]
</tbody>[/TABLE]



I found this but it combines my reasons in the same cell with a space in-between each value. But this code is too advanced for me to pick apart :(.

<code class="vb plain">CombineRows()</code>
<code class="vb comments">'Update 20131202</code>
<code class="vb keyword">Dim</code> <code class="vb plain">WorkRng </code><code class="vb keyword">As</code> <code class="vb plain">Range</code>
<code class="vb keyword">Dim</code> <code class="vb plain">Dic </code><code class="vb keyword">As</code> <code class="vb keyword">Variant</code>
<code class="vb keyword">Dim</code> <code class="vb plain">arr </code><code class="vb keyword">As</code> <code class="vb keyword">Variant</code>
<code class="vb keyword">On</code> <code class="vb keyword">Error</code> <code class="vb keyword">Resume</code> <code class="vb keyword">Next</code>
<code class="vb plain">xTitleId = </code><code class="vb string">"KutoolsforExcel"</code>
<code class="vb keyword">Set</code> <code class="vb plain">WorkRng = Application.Selection</code>
<code class="vb keyword">Set</code> <code class="vb plain">WorkRng = Application.InputBox(</code><code class="vb string">"Range"</code><code class="vb plain">, xTitleId, WorkRng.Address, Type:=8)</code>
<code class="vb keyword">Set</code> <code class="vb plain">Dic = CreateObject(</code><code class="vb string">"Scripting.Dictionary"</code><code class="vb plain">)</code>
<code class="vb plain">arr = WorkRng.Value</code>
<code class="vb keyword">For</code> <code class="vb plain">i = 1 </code><code class="vb keyword">To</code> <code class="vb plain">UBound(arr, 1)</code>
<code class="vb spaces"> </code><code class="vb plain">xvalue = arr(i, 1)</code>
<code class="vb spaces"> </code><code class="vb keyword">If</code> <code class="vb plain">Dic.Exists(xvalue) </code><code class="vb keyword">Then</code>
<code class="vb spaces"> </code><code class="vb plain">Dic(arr(i, 1)) = Dic(arr(i, 1)) & </code><code class="vb string">" "</code> <code class="vb plain">& arr(i, 2)</code>
<code class="vb spaces"> </code><code class="vb keyword">Else</code>
<code class="vb spaces"> </code><code class="vb plain">Dic(arr(i, 1)) = arr(i, 2)</code>
<code class="vb spaces"> </code><code class="vb keyword">End</code> <code class="vb keyword">If</code>
<code class="vb keyword">Next</code>
<code class="vb plain">Application.ScreenUpdating = </code><code class="vb keyword">False</code>
<code class="vb plain">WorkRng.ClearContents</code>
<code class="vb plain">WorkRng.Range(</code><code class="vb string">"A1"</code><code class="vb plain">).Resize(Dic.Count, 1) = Application.WorksheetFunction.Transpose(Dic.keys)</code>
<code class="vb plain">WorkRng.Range(</code><code class="vb string">"B1"</code><code class="vb plain">).Resize(Dic.Count, 1) = Application.WorksheetFunction.Transpose(Dic.items)</code>
<code class="vb plain">Application.ScreenUpdating = </code><code class="vb keyword">True</code>
<code class="vb keyword">End</code> <code class="vb keyword">Sub</code>
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Try:
Code:
Sub mergeLikeRows2()
    Application.ScreenUpdating = False
    Dim LastRow As Long
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Dim request As Range, rng As Range
    Dim secVisRow As Long
    Dim lCol As Long
    Sheets("Sheet1").Range("A1:A" & LastRow).AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Range("A1:A" & LastRow), Unique:=True
    Set rnguniques = Sheets("Sheet1").Range("A2:A" & LastRow).SpecialCells(xlCellTypeVisible)
    For Each request In rnguniques
        Range("A1:A" & LastRow).AutoFilter Field:=1, Criteria1:=request
        secVisRow = Range("A2", Cells(Rows.Count, "A").End(xlUp)).SpecialCells(xlCellTypeVisible).Cells(1, 1).Row + 1
        For Each rng In Range("A" & secVisRow & ":A" & LastRow).SpecialCells(xlCellTypeVisible)
            lCol = Cells(secVisRow - 1, Columns.Count).End(xlToLeft).Column + 1
            Range(Cells(rng.Row, 2), Cells(rng.Row, lCol)).Copy Cells(secVisRow - 1, lCol)
            'rng.EntireRow.Delete
        Next rng
    Next request
    If ActiveSheet.AutoFilterMode = True Then ActiveSheet.AutoFilterMode = False
    lCol = Cells(1, Columns.Count).End(xlToLeft).Column
    Range(Cells(1, 1), Cells(LastRow, lCol)).RemoveDuplicates Columns:=1, Header:=xlYes
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thanks Mumps, this is definitely closer. I made a few changes, but when I run it now, it not only grabs the duplicate request reasons, but also the non duplicate request reasons. For the example above on request 123 I would now have a,b,c,a,a,b as reasons.

Sub mergeLikeRows2()
Application.ScreenUpdating = False
Dim LastRow As Long
LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Dim request As Range, rng As Range
Dim secVisRow As Long
Dim lCol As Long
Sheets("Sheet1").Range("A2:A" & LastRow).AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Range("A2:A" & LastRow), Unique:=True
Set rnguniques = Sheets("Sheet1").Range("A2:A" & LastRow).SpecialCells(xlCellTypeVisible)
For Each request In rnguniques
Range("A1:A" & LastRow).AutoFilter Field:=1, Criteria1:=request
secVisRow = Range("A2", Cells(Rows.Count, "A").End(xlUp)).SpecialCells(xlCellTypeVisible).Cells(1, 1).Row + 1
For Each rng In Range("A" & secVisRow & ":A" & LastRow).SpecialCells(xlCellTypeVisible)
lCol = Cells(secVisRow - 1, Columns.Count).End(xlToLeft).Column + 1
Range(Cells(rng.Row, 2), Cells(rng.Row, lCol)).Copy Cells(secVisRow - 1, lCol)
'rng.EntireRow.Delete
Next rng
Next request
If ActiveSheet.AutoFilterMode = True Then ActiveSheet.AutoFilterMode = False
lCol = Cells(1, Columns.Count).End(xlToLeft).Column
Range(Cells(1, 1), Cells(LastRow, lCol)).RemoveDuplicates Columns:=1, Header:=xlYes
Application.ScreenUpdating = True
End Sub
 
Upvote 0
I tried the macro on some dummy data and it worked as you requested. What changes did you make? Unless I'm missing something, the macro you posted looks the same as the one I suggested. Also, when posting code, please use code tags. You can do this by highlighting the code and then clicking the # sign in the menu at the top of the response window. By the way, you can delete the following line of code:
Code:
 'rng.EntireRow.Delete
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,301
Members
452,633
Latest member
DougMo

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