Combining multiple rows into a single row.

Detectiveclem

Active Member
Joined
May 31, 2014
Messages
320
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. MacOS
Hi, I have a spreadsheet which has circa 23,000 rows of data.


Column ‘A’ has the client ID, this being a unique number for each client. However there are multi entries for the same client. In column J has a monetary amount.


What I would like to achieve is to merge all the same relating to the same client on a single row and provide a total monetary figure in column J (total of all entries relating to that client). The spreadsheet uses Column A:R, with the data relating to that client being identical with the except of Column J which I’ve mentioned.


Help with this would be greatly appreciated
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Do you have any formulae or formatting on the sheet?
 
Upvote 0
Ok, how about
VBA Code:
Sub Detectiveclem()
   Dim Ary As Variant, Nary As Variant
   Dim r As Long, c As Long, nr As Long
   
   Ary = Sheets("Sheet1").Range("A1").CurrentRegion.Value2
   ReDim Nary(1 To UBound(Ary), 1 To UBound(Ary, 2))
   With CreateObject("scripting.dictionary")
      For r = 1 To UBound(Ary)
         If Not .exists(Ary(r, 1)) Then
            nr = nr + 1
            .Add Ary(r, 1), nr
            For c = 1 To UBound(Ary, 2)
               Nary(nr, c) = Ary(r, c)
            Next c
         Else
            Nary(.Item(Ary(r, 1)), 10) = Nary(.Item(Ary(r, 1)), 10) + Ary(r, 10)
         End If
      Next r
   End With
   Sheets("Sheet2").Range("A1").Resize(nr, UBound(Ary, 2)).Value = Nary
End Sub
This currently outputs the data on Sheet2, just to check it works. It can easily be changed to clear sheet1 & then put the data back in.
 
Upvote 0
Hi Fluuf,

Tried this and altered the sheet name to the correct name, but the debugger stopped at the final line Sheets("Sheet2").Range("A1").Resize(nr, UBound(Ary, 2)).Value = Nary
 
Upvote 0
This is the full code,

Sub Rowmerge()
Dim Ary As Variant, Nary As Variant
Dim r As Long, c As Long, nr As Long

Ary = Sheets("PSCmerge").Range("A1").CurrentRegion.Value2
ReDim Nary(1 To UBound(Ary), 1 To UBound(Ary, 2))
With CreateObject("scripting.dictionary")
For r = 1 To UBound(Ary)
If Not .exists(Ary(r, 1)) Then
nr = nr + 1
.Add Ary(r, 1), nr
For c = 1 To UBound(Ary, 2)
Nary(nr, c) = Ary(r, c)
Next c
Else
Nary(.Item(Ary(r, 1)), 10) = Nary(.Item(Ary(r, 1)), 10) + Ary(r, 10)
End If
Next r
End With
Sheets("Sheet2").Range("A1").Resize(nr, UBound(Ary, 2)).Value = Nary
End Sub
 
Upvote 0
Do you have a sheet called sheet2?
 
Upvote 0
No just Sheet1 and other named sheets. Do I have to create a sheet2?
 
Upvote 0
It would be better, that way you can check that everything has worked, without destroying your data.
 
Upvote 0

Forum statistics

Threads
1,214,859
Messages
6,121,963
Members
449,059
Latest member
oculus

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