Merge cells from different rows depending on certain criteria

gemf89

New Member
Joined
Aug 1, 2018
Messages
1
Hello. I have been looking for an answer but obviously it is hard to find a case exactly like mine and I dont have the technical nous to change anything without screwing it up. If you can help I will be very grateful.

I get an auto-generated spreadsheet from a software. This is a sales software where I can find many orders with different products and there is row for every product even in the same order. I need to merge those rows into only one for every order.

Original spreadsheet
[TABLE="class: grid, width: 500, align: left"]
<tbody>[TR]
[TD]Client[/TD]
[TD]Order No. [/TD]
[TD]Product[/TD]
[TD]Price[/TD]
[/TR]
[TR]
[TD]Robert[/TD]
[TD]001[/TD]
[TD]Apples[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD]Robert[/TD]
[TD]001[/TD]
[TD]Oranges[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]Max[/TD]
[TD]002[/TD]
[TD]Apples[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD]Max[/TD]
[TD]002[/TD]
[TD]Oranges[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]Max[/TD]
[TD]003[/TD]
[TD]Apples[/TD]
[TD]2[/TD]
[/TR]
</tbody>[/TABLE]











What I need is to automatically detect every single order No. and merge those rows into one. Like these:

[TABLE="class: grid, width: 500, align: left"]
<tbody>[TR]
[TD]Client[/TD]
[TD]Order No. [/TD]
[TD]Product[/TD]
[TD]Price[/TD]
[/TR]
[TR]
[TD]Robert[/TD]
[TD]001[/TD]
[TD]Apples-Oranges[/TD]
[TD]2-1[/TD]
[/TR]
[TR]
[TD]Max[/TD]
[TD]002[/TD]
[TD]Apples-Oranges[/TD]
[TD]2-1[/TD]
[/TR]
[TR]
[TD]Max[/TD]
[TD]003[/TD]
[TD]Apples[/TD]
[TD]2[/TD]
[/TR]
</tbody>[/TABLE]








thank you!
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
See next code
Code:
Option Explicit


Sub MergeCells()
Const FR = 2
Dim DataDic   As Object
Set DataDic = CreateObject("Scripting.Dictionary")
Dim Rg As Range
Dim I  As Integer
Dim AAA, BBB
    Columns(4).Cells.NumberFormat = "@"
    For I = FR To Cells(Rows.Count, 1).End(3).Row
        With DataDic
            If (.exists(Cells(I, 1) & Cells(I, 2))) Then
                Cells(.Item(Cells(I, 1) & Cells(I, 2)), 3) = Cells(.Item(Cells(I, 1) & Cells(I, 2)), 3) & " - " & Cells(I, 3)
                Cells(.Item(Cells(I, 1) & Cells(I, 2)), 4) = Cells(.Item(Cells(I, 1) & Cells(I, 2)), 4) & " - " & Cells(I, 4)
                Cells(I, 1) = ""
            Else
                .Item(Cells(I, 1) & Cells(I, 2)) = I
            End If
            AAA = .keys: BBB = .items
        End With
    Next
    Columns(1).Cells.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    MsgBox ("Job Done")
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,265
Members
452,627
Latest member
KitkatToby

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