Combine multiple cell values when same ID value is found in sheet

Patty3-4

New Member
Joined
Feb 5, 2013
Messages
35
Office Version
  1. 2016
Platform
  1. Windows
Good afternoon,

I'm requesting help from the brilliant minds on this forum for my newest project.

I need to combine column D (CATEGORY) into a column E (FINAL CATEGORY), WHEN the column A (ID NUMBER) is the same, and WHEN column D (CATEGORY) are different.

If column D (CATEGORY) is the same for the same ID NUMBER, the CATEGORY is to only show once.
Any one ID NUMBER can have an unlimited number of rows. Usually the data has about 10k rows of data.

My final result will be a pivoted summary of column E (FINAL CATEGORY), which will show me the final categories created and number of people (COUNT) in them.

Any help you could provide, I/we'd truly really appreciate it.
 

Attachments

  • Sample.PNG
    Sample.PNG
    68.6 KB · Views: 35

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Hi Patty3-4,

maybe use a procedure like

VBA Code:
Public Sub MrE_1225755_1616D12()
' https://www.mrexcel.com/board/threads/combine-multiple-cell-values-when-same-id-value-is-found-in-sheet.1225755/
' Created: 20221231
' By:      HaHoBe

'/// PLEASE NOTE !!!!
'/// Please note: ALWAYS run the macros on a copy of your Workbook !!!!
'/// PLEASE NOTE !!!!

Dim arr
Dim lngCounter As Long
Dim lngStart As Long
Dim strTemp As String
Dim lngArr As Long
Dim lngLast As Long

Worksheets("Data").Copy after:=Sheets(Sheets.Count)
ActiveSheet.Name = "Data" & Format(Now, "_yymmdd_hhmmss")

lngLast = Cells(Rows.Count, 1).End(xlUp).Row

arr = Range("A2:F" & lngLast).Value
lngStart = LBound(arr, 1)
For lngCounter = LBound(arr, 1) + 1 To UBound(arr, 1)
  If arr(lngCounter, 1) <> arr(lngCounter - 1, 1) Then
    If Right(strTemp, 1) = "_" Then strTemp = Left(strTemp, Len(strTemp) - 1)
    For lngArr = lngStart To lngCounter
      arr(lngArr, 5) = strTemp
    Next lngArr
    arr(lngStart, 6) = 1
    lngStart = lngCounter
    strTemp = arr(lngCounter, 4) & "_"
  Else
    If InStr(1, strTemp, arr(lngCounter - 1, 4)) = 0 Then
      strTemp = strTemp & arr(lngCounter - 1, 4) & "_"
    End If
    If InStr(1, strTemp, arr(lngCounter, 4)) = 0 Then
      strTemp = strTemp & arr(lngCounter, 4) & "_"
    End If
  End If
Next lngCounter

If Right(strTemp, 1) = "_" Then strTemp = Left(strTemp, Len(strTemp) - 1)
For lngArr = lngStart To UBound(arr, 1)
  arr(lngArr, 5) = strTemp
Next lngArr
arr(lngStart, 6) = 1

Range("A2:F" & lngLast).Value = arr
End Sub

Please alter the name of the worksheet holding the data to suit.

Ciao,
Holger
 
Upvote 0
Solution
Good morning! Thank you! I'm not too familiar with VBA but this went in and worked great! This will make the weekly stat report 100% easier. I like how it creates another sheet too. Just beautiful!
 
Upvote 0
Just want to check if the Category values will always occur in the same order and just what your requirement is.
For example, I ran the above code on the sample data shown in columns A, D and achieved these results. The two coloured sections are made up of exactly the same Category values but would not get combined if you make a Pivot Table or otherwise do a count. Is that what you want or should those two coloured sections show the three categories in the same order as each other?

Patty3-4.xlsm
ADEF
1IDCategoryFinalCount
2934YellowYellow1
3934YellowYellow
4934YellowYellow
545038GreenGreen_Yellow_Pink1
645038YellowGreen_Yellow_Pink
745038YellowGreen_Yellow_Pink
845038PinkGreen_Yellow_Pink
9328GreenGreen1
1044PinkPink_Green_Yellow1
1144GreenPink_Green_Yellow
1244YellowPink_Green_Yellow
136666GreenGreen_Yellow1
146666YellowGreen_Yellow
156666YellowGreen_Yellow
166666GreenGreen_Yellow
Data_230105_093733
 
Upvote 0

Forum statistics

Threads
1,214,824
Messages
6,121,784
Members
449,049
Latest member
greyangel23

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