Can someone please help with a VBA that will recognise a unique id for the row and then combine info from multiple entries

tbancroft

New Member
Joined
Aug 14, 2016
Messages
3
Hi Excel Gurus!

I have an excel doc with 'customer information' whereby there may be multiple entries for each unique ID (or customer). The worksheet has some multiple rows which contain individual data entries (1 row may have an email address and unique ID, another may have a first and second name and the same unique ID).
I'm looking for a way to combine all the info so that each unique ID is only listed once, and all the relevant fields for that ID are collated.

If its relevant my sheet runs from 'A' to 'U' with the unique ID being in column A. The sheet is approx 50,000 rows.

Thanks in advance for your help!!
 
Hi Excel Gurus!

I have an excel doc with 'customer information' whereby there may be multiple entries for each unique ID (or customer). The worksheet has some multiple rows which contain individual data entries (1 row may have an email address and unique ID, another may have a first and second name and the same unique ID).
I'm looking for a way to combine all the info so that each unique ID is only listed once, and all the relevant fields for that ID are collated.

If its relevant my sheet runs from 'A' to 'U' with the unique ID being in column A. The sheet is approx 50,000 rows.
Do you have a header row (if more than one, how many)?

Is your data sorted by Column A? If not, can it be?

Just checking... for each unique ID, from Column B onward, there is guaranteed to be only one entry per column no matter how many rows are occupied by the unique ID, correct?
 
Upvote 0
Hi Rick,
1 header row
Yes, it is sorted by column A (descending)
No - Some of the cells may have repeat information, but the repeat info (if it is repeated) is the same in both cells

Thanks for your help!!
 
Upvote 0
No - Some of the cells may have repeat information, but the repeat info (if it is repeated) is the same in both cells
Okay, that kills the simple approach I was hoping to take. Let me see what I (or others) can come up with.
 
Upvote 0
See if this macro does what you want. Note that I assume your data is on Sheet1 (green text) and that you want your output to go to Sheet2 (red text, you might have to scroll down to see it)... change as needed (if you change the red text to the same sheet name as the green text, the new data will completely overwrite the old data).
Code:
Sub CombineDataPerUniqueID()
  Dim R As Long, RR As Long, C As Long, X As Long, Rw As Long, LastRow As Long
  Dim Data As Variant, Result As Variant
  With Sheets("[B][COLOR="#008000"]Sheet1[/COLOR][/B]")
    LastRow = .Columns("A:U").Find("*", , xlValues, , xlRows, xlPrevious).Row
    Data = .Range("A1:U" & LastRow + 1)
  End With
  ReDim Result(1 To UBound(Data), 1 To 21)
  For R = 2 To UBound(Data)
    If Data(R, 1) <> Data(R - 1, 1) Then
      X = X + 1
      Result(X, 1) = Data(R, 1)
      Result(X, 2) = R
    End If
  Next
  X = 0
  For R = 2 To UBound(Data)
    X = X + 1
    Rw = Result(X, 2)
    Result(X, 2) = ""
    For C = 2 To 21
      For RR = Rw To Result(X + 1, 2) - 1
        If Len(Data(RR, C)) Then
          Result(X, C) = Data(RR, C)
          Exit For
        End If
      Next
    Next
    R = Result(X + 1, 2)
  Next
  Result(X + 1, 2) = ""
  With Sheets("[B][COLOR="#FF0000"]Sheet2[/COLOR][/B]")
    .Cells.Clear
    .Range("A1").Resize(UBound(Result, 1), UBound(Result, 2)) = Result
  End With
End Sub
 
Last edited:
Upvote 0

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