Macro: Lookup Max of comma separated values

MaxPower33

New Member
Joined
May 29, 2013
Messages
12
I wondering what the best approach might be to lookup multiple comma separated values to find the max value of each string.

I can do this by first splitting out the comma delimted values and using multiple lookup formula to retrieve a value for each and then max of the results.... but i'm wondering might a macro be better since data volume can be 100k+ lines.

The comma separated values (keywords) are reported on a sheet called 'Keywords' for 3 different values
Values are reported in col C, D, E as per below example

Sheet = "Keywords" -->

ABCDE
1IDGeoSegfuncocc
2270784189United States180, 190, 200, 160, 300, 250abc, ffff, ggg, aaa, bbb, hhh400, 3000, 200, 10, 20
3270783575United States101, 102, 103, 104, 105iii, abcd, efgh, ijk20, 10, 60, 800, 1000
4270783576EMEA300, 250iii, abcd,20, 10, 60,
5270783580NAMER101, 102iii, abcd800, 1000

<colgroup><col style="width: 21pt; mso-width-source: userset; mso-width-alt: 1024;" width="28"> <col style="width: 61pt; mso-width-source: userset; mso-width-alt: 2962;" width="81"> <col style="width: 74pt; mso-width-source: userset; mso-width-alt: 3584;" width="98"> <col style="width: 150pt; mso-width-source: userset; mso-width-alt: 7314;" width="200"> <col style="width: 132pt; mso-width-source: userset; mso-width-alt: 6436;" width="176"> <col style="width: 142pt; mso-width-source: userset; mso-width-alt: 6912;" width="189"> <tbody>
</tbody>

<tbody>
</tbody>


Values are reported on a 2nd sheet (Lookup_Values) with a value against each combination of Geo+seg/func/occ

The lookup needs to lookup a concatination of Geo+seg/func/occ to find the corresponding value in Col D/I/N on the Lookup_Values sheet.

Sheet = "Lookup_Values" -->
ABCDEFGHIJKLMN
1GeoSeg IDLookup_ValValueGeofuncLookup_ValValueGeooccLookup_ValValue
2United States101United States1010.2United StatesabcUnited Statesabc0.1United States10United States100.1
3United States102United States1020.5United StatesffffUnited Statesffff0.22United States20United States200.22
4United States103United States1030.3United StatesgggUnited Statesggg0.33United States60United States600.33
5United States104United States1040.2United StatesaaaUnited Statesaaa0.1United States200United States2000.1
6United States105United States1050.6United StatesbbbUnited Statesbbb0.15United States400United States4000.15
7United States106United States1060.7United StateshhhUnited Stateshhh0.22United States800United States8000.22
8United States107United States1070.75United StatesiiiUnited Statesiii0.25United States1000United States10000.25
9United States108United States1080.85United StatesabcdUnited Statesabcd0.4United States3000United States30000.4
10United States109United States1090.1United StatesefghUnited Statesefgh0.5EMEA10EMEA100.25
11United States110United States1100.15United StatesijkUnited Statesijk0.36EMEA20EMEA200.4
12United States160United States1600.25EMEAabcEMEAabc0.1EMEA60EMEA600.25
13United States180United States1800.35EMEAffffEMEAffff0.15EMEA200EMEA2000.4
14United States190United States1900.45EMEAgggEMEAggg0.22EMEA400EMEA4000.25
15United States200United States2000.55EMEAaaaEMEAaaa0.25EMEA800EMEA8000.4
16United States250United States2500.65EMEAbbbEMEAbbb0.15EMEA1000EMEA10000.25
17United States300United States3000.7EMEAhhhEMEAhhh0.22EMEA3000EMEA30000.4
18EMEA101EMEA1010.2EMEAiiiEMEAiii0.25NAMER10NAMER100.5
19EMEA102EMEA1020.5EMEAabcdEMEAabcd0.4NAMER20NAMER200.2
20EMEA103EMEA1030.45EMEAefghEMEAefgh0.5NAMER60NAMER600.1
21EMEA104EMEA1040.55EMEAijkEMEAijk0.36NAMER200NAMER2000.2
22EMEA105EMEA1050.65NAMERabcNAMERabc0.15NAMER400NAMER4000.1
23EMEA106EMEA1060.7NAMERffffNAMERffff0.22NAMER800NAMER8000.3
24EMEA107EMEA1070.45NAMERgggNAMERggg0.25NAMER1000NAMER10000.4
25EMEA108EMEA1080.55NAMERaaaNAMERaaa0.4NAMER3000NAMER30000.5
26EMEA109EMEA1090.65NAMERbbbNAMERbbb0.15
27EMEA110EMEA1100.15NAMERhhhNAMERhhh0.22
28EMEA160EMEA1600.25NAMERiiiNAMERiii0.25
29EMEA180EMEA1800.35NAMERabcdNAMERabcd0.4
30EMEA190EMEA1900.45NAMERefghNAMERefgh0.5
31EMEA200EMEA2000.55NAMERijkNAMERijk0.36
32EMEA250EMEA2500.65
33EMEA300EMEA3000.7
34NAMER101NAMER1010.2
35NAMER102NAMER1020.5
36NAMER103NAMER1030.45
37NAMER104NAMER1040.55
38NAMER105NAMER1050.65
39NAMER106NAMER1060.7
40NAMER107NAMER1070.45
41NAMER108NAMER1080.55
42NAMER109NAMER1090.65
43NAMER110NAMER1100.15
44NAMER160NAMER1600.25
45NAMER180NAMER1800.35
46NAMER190NAMER1900.45
47NAMER200NAMER2000.55
48NAMER250NAMER2500.65
49NAMER300NAMER3000.7

<colgroup><col style="width: 26pt; mso-width-source: userset; mso-width-alt: 1280;" width="35"> <col style="width: 68pt; mso-width-source: userset; mso-width-alt: 3291;" width="90"> <col style="width: 48pt;" width="64"> <col style="width: 84pt; mso-width-source: userset; mso-width-alt: 4096;" width="112"> <col style="width: 48pt;" span="2" width="64"> <col style="width: 74pt; mso-width-source: userset; mso-width-alt: 3620;" width="99"> <col style="width: 46pt; mso-width-source: userset; mso-width-alt: 2230;" width="61"> <col style="width: 84pt; mso-width-source: userset; mso-width-alt: 4096;" width="112"> <col style="width: 48pt;" span="2" width="64"> <col style="width: 68pt; mso-width-source: userset; mso-width-alt: 3291;" width="90"> <col style="width: 41pt; mso-width-source: userset; mso-width-alt: 2011;" width="55"> <col style="width: 89pt; mso-width-source: userset; mso-width-alt: 4352;" width="119"> <col style="width: 48pt;" width="64"> <tbody>
</tbody>

<tbody>
</tbody>

So for example. When you lookup the values for the first ID (270784189), the results might report as this:

FGH
Segfuncocc
0.35, 0.45, 0.55, 0.7, 0.650.1, 0.22, 0.33, 0.1, 0.15, 0.220.15, 0.4, 0.1, 0.1, 0.22

<colgroup><col style="width: 117pt; mso-width-source: userset; mso-width-alt: 5705;" width="156"> <col style="width: 137pt; mso-width-source: userset; mso-width-alt: 6656;" width="182"> <col style="width: 106pt; mso-width-source: userset; mso-width-alt: 5156;" width="141"> <tbody>
</tbody>

<tbody>
</tbody>

So for example, the final Max result for the first ID (270784189) would be:

IJK
Max_SegMax_funcMax_occ
0.70.330.4

<colgroup><col style="width: 47pt; mso-width-source: userset; mso-width-alt: 2267;" width="62"> <col style="width: 50pt; mso-width-source: userset; mso-width-alt: 2450;" width="67"> <col style="width: 45pt; mso-width-source: userset; mso-width-alt: 2194;" width="60"> <tbody>
</tbody>

<tbody>
</tbody>

These results don't necessarily have to report as comma delimted resutls as per example. Alternatively, they could be split as text to colums across 3 separate worksheets (1 for seg/occ/func). This may lend itself better to concatenting values with geo.

The max number of comma delimted values per seg/occ/func is 100.

Any suggestions much appreciated!
Thanks!!! :eek:
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Results in columns "F to K" of Sheet "Keywords"
If results are correct, where do you want the resultd placed ???
Code:
[COLOR="Navy"]Sub[/COLOR] MG03Sep55
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] Seg [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] func [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] occ [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] UnQ [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]Dim[/COLOR] Keyword [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] Dic [COLOR="Navy"]As[/COLOR] Object
[COLOR="Navy"]Dim[/COLOR] Rstr [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Dim[/COLOR] oMax [COLOR="Navy"]As[/COLOR] Double
[COLOR="Navy"]Dim[/COLOR] oCol [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Integer[/COLOR]
[COLOR="Navy"]Dim[/COLOR] Ac [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Integer[/COLOR]
[COLOR="Navy"]Dim[/COLOR] s [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]Dim[/COLOR] Dstr [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]With[/COLOR] Sheets("Lookup_Values")
 [COLOR="Navy"]Set[/COLOR] Seg = .Range(.Range("B2"), .Range("B" & Rows.Count).End(xlUp))
 [COLOR="Navy"]Set[/COLOR] func = .Range(.Range("G2"), .Range("G" & Rows.Count).End(xlUp))
 [COLOR="Navy"]Set[/COLOR] occ = .Range(.Range("L2"), .Range("L" & Rows.Count).End(xlUp))
[COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]Set[/COLOR] Rng = Union(Seg, func, occ)
[COLOR="Navy"]Set[/COLOR] Dic = CreateObject("scripting.dictionary")
Dic.CompareMode = vbTextCompare
    [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
        UnQ = Dn.Column & Dn.Offset(, -1) & Dn
            Dic.Item(UnQ) = Dn.Offset(, 2)
    [COLOR="Navy"]Next[/COLOR]
[COLOR="Navy"]With[/COLOR] Sheets("Keywords")
 [COLOR="Navy"]Set[/COLOR] Keyword = .Range(.Range("A2"), .Range("A" & Rows.Count).End(xlUp))
[COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Keyword
    [COLOR="Navy"]For[/COLOR] Ac = 2 To 4
        [COLOR="Navy"]Select[/COLOR] [COLOR="Navy"]Case[/COLOR] True
            [COLOR="Navy"]Case[/COLOR] Ac = 2: oCol = 2
            [COLOR="Navy"]Case[/COLOR] Ac = 3: oCol = 7
            [COLOR="Navy"]Case[/COLOR] Ac = 4: oCol = 12
        [COLOR="Navy"]End[/COLOR] Select
        Rstr = Split(Dn.Offset(, Ac), ",")
        [COLOR="Navy"]For[/COLOR] n = 0 To UBound(Rstr)
            Dstr = oCol & Dn.Offset(, 1) & Trim(Rstr(n))
            oMax = Application.Max(oMax, Dic.Item(Dstr))
            s = s & Dic.Item(Dstr) & ","
        [COLOR="Navy"]Next[/COLOR] n
            Dn.Offset(, Ac + 3) = oMax
            Dn.Offset(, Ac + 6) = s
            oMax = 0
            s = vbNullString
    [COLOR="Navy"]Next[/COLOR] Ac
[COLOR="Navy"]Next[/COLOR] Dn
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Results in columns "F to K" of Sheet "Keywords"
If results are correct, where do you want the resultd placed ???

Amazing!!
Yes, this seems to have done the job... really efficient too when i tested it with a large dataset .;)
I added a 4th Range to the code as per below.
I assume I could potentially add several more ranges in the same way if that was requried?...

I'm not entirely sure where best to report the results yet so in F to K (now G to J) is perfect for now.

Code:
Dim Rng As Range, Dn As Range, n As Long
Dim Seg As Range
Dim func As Range
Dim occ As Range
Dim ind As Range
Dim UnQ As String
Dim Keyword As Range
Dim Dic As Object
Dim Rstr As Variant
Dim oMax As Double
Dim oCol As Integer
Dim Ac As Integer
Dim s As String
Dim Dstr As String
With Sheets("Lookup_Values")
 Set Seg = .Range(.Range("B2"), .Range("B" & Rows.Count).End(xlUp))
 Set func = .Range(.Range("G2"), .Range("G" & Rows.Count).End(xlUp))
 Set occ = .Range(.Range("L2"), .Range("L" & Rows.Count).End(xlUp))
 Set ind = .Range(.Range("Q2"), .Range("Q" & Rows.Count).End(xlUp))
End With
Set Rng = Union(Seg, func, occ, ind)
Set Dic = CreateObject("scripting.dictionary")
Dic.CompareMode = vbTextCompare
    For Each Dn In Rng
        UnQ = Dn.Column & Dn.Offset(, -1) & Dn
            Dic.Item(UnQ) = Dn.Offset(, 2)
    Next
With Sheets("Keywords")
 Set Keyword = .Range(.Range("A2"), .Range("A" & Rows.Count).End(xlUp))
End With
For Each Dn In Keyword
    For Ac = 2 To 5
        Select Case True
            Case Ac = 2: oCol = 2
            Case Ac = 3: oCol = 7
            Case Ac = 4: oCol = 12
            Case Ac = 5: oCol = 17
        End Select
        Rstr = Split(Dn.Offset(, Ac), ",")
        For n = 0 To UBound(Rstr)
            Dstr = oCol & Dn.Offset(, 1) & Trim(Rstr(n))
            oMax = Application.Max(oMax, Dic.Item(Dstr))
            s = s & Dic.Item(Dstr) & ","
        Next n
            Dn.Offset(, Ac + 4) = oMax
            Dn.Offset(, Ac + 8) = s
            oMax = 0
            s = vbNullString
    Next Ac
Next Dn
End Sub

Thanks Mick, that's truely awesome!
 
Upvote 0
I think you could add as many more ranges as you want.
Thanks for the reply
Regrds Mick
 
Upvote 0

Forum statistics

Threads
1,214,998
Messages
6,122,639
Members
449,093
Latest member
Ahmad123098

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