VBA or formula to group values for each unique reference

Trebor8484

Board Regular
Joined
Oct 27, 2018
Messages
69
Office Version
  1. 2013
Platform
  1. Windows
Hi,

I have a table with a list of job references, after sorting the table by job ref then date I am trying to group the job type prefixes into as single cell.

Does anyone have any suggestions for doing this either with VBA or a formula please see example below in the end column and file attached for reference.

Capture.JPG


easyupload.io

Thanks
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
I have solved this for anone who may be interested in future.

Needs to be an array formula if using Excel prior to 365 version

Excel Formula:
=IF([@[Unique Ref]]="","",TEXTJOIN(" > ", TRUE, IF([@[Unique Ref]]=[Unique Ref],[Job Jype],"")))

And a UDF if using a version of Excel that doesn't support TEXTJOIN

VBA Code:
'Name user defined function and define parameters
Function Lookup_concat(Search_string As String, Search_in_col As Range, Return_val_col As Range)

    'Dimension variables and declare data types
    Dim i As Long
    Dim result As String
    Dim delim As String
    
    'Set the seperator value
    delim = " > "
    'delim = " "

    'Iterate through each cell in search column
    For i = 1 To Search_in_col.Count
        'Check if cell is equal to search string
        If Search_in_col.Cells(i, 1) = Search_string Then
            'Concatenate corresponding value on the same row to the result variable
            'result = result & " " & Return_val_col.Cells(i, 1).Value
            result = result & delim & Return_val_col.Cells(i, 1).Value
        End If
    'Continue with next cell
    Next

    'Return variable to worksheet
    If delim <> " " Then
        Lookup_concat = Trim(Mid(result, Len(delim), Len(result)))
    Else
        Lookup_concat = Trim(result)
    End If
    
End Function
 
Upvote 0
And a UDF if using a version
Here is another UDF version that I think does the same job.

VBA Code:
Function ConcatJobType(rRef As Range, rJT As Range, sRef As String) As String
  ConcatJobType = Mid(Replace(Join(Filter(Application.Transpose(Evaluate(rRef.Address & "&""> ""&" & rJT.Address)), sRef)), sRef, ""), 3)
End Function

Trebor8484.xlsm
ABCDE
1Unique RefCase CountAssigned TimeJob JypeJob Sequence
261520306447815/10/2021 8:13GG > v
361520306447807/10/2021 8:28vG > v
461520308275917/10/2021 17:53GG > G
561520308275907/10/2021 19:32GG > G
6615203200385127/11/2021 9:53GG > v
7615203200385027/11/2021 13:52vG > v
861520330943315/10/2021 9:23vv > v
961520330943305/10/2021 11:13vv > v
1061520333411217/10/2021 17:51vv > G
1161520333411208/10/2021 8:23Gv > G
1261520333411017/10/2021 8:28GG > v > G
1361520333411007/10/2021 17:53vG > v > G
1461520333411007/10/2021 19:32GG > v > G
Sheet1
Cell Formulas
RangeFormula
E2:E14E2=ConcatJobType([Unique Ref],[Job Jype],[@[Unique Ref]])
 
Upvote 0
Thanks for the shorter one line UDF :)

One problem with all of these and the formula is that they run very slowly on 45,000 rows of data.

Is there any way I can store these as keys in a dictionary and write them to the sheet in one go at the end?
 
Upvote 0
Judging by your post 2 formula, I have assumed the data is in a formal table. Mine is called "Table2" so adjust the code to match your table name.
I have also assumed that the (empty) "Job Sequence" column already exists in the table.
Test with a copy of your workbook.

VBA Code:
Sub JobSequence()
  Dim d As Object
  Dim Ref As Variant, JobJype As Variant, JobSeq As Variant
  Dim i As Long
 
  Set d = CreateObject("Scripting.Dictionary")
  Ref = Range("Table2[Unique Ref]").Value
  JobJype = Range("Table2[Job Jype]").Value
  ReDim JobSeq(1 To UBound(Ref), 1 To 1)
  For i = 1 To UBound(Ref)
      If Len(Ref(i, 1)) > 0 Then d(Ref(i, 1)) = d(Ref(i, 1)) & " > " & JobJype(i, 1)
  Next i
  For i = 1 To UBound(Ref)
    If Len(Ref(i, 1)) > 0 Then JobSeq(i, 1) = Mid(d(Ref(i, 1)), 4)
  Next i
  Range("Table2[Job Sequence]").Value = JobSeq
End Sub
 
Upvote 0
Solution
Thanks, works perfectly. Ran in 8 seconds instead of 10 minutes when using the UDF's. :)
 
Upvote 0
Sounds like a good improvement. :)
Thanks for letting us know. (y)
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,391
Members
448,957
Latest member
Hat4Life

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