VBA: find all matches of one list in each cell of another list and create combined rank

fxrexcel

New Member
Joined
Aug 11, 2018
Messages
18
I have the following problem with VBA:

I have a list of names of persons with a rank (either "1" or "2", see below) and I want to search their number of occurrences in another table and add up the ranks.

Person Data:

PersonRank
A1
B2
K2
P1
U2
T1
......

<tbody>
</tbody>


Company Data:

  • red is what needs to be written in Col "Combined Rank")
  • In my Company Data, the names of the persons data are sometimes followed by random brackets and/or spaces and then brackets

CompanyPersonsCombined Rank
C1A(), B (..)3 [= Rank(A)+Rank(B)=1 + 2]
C2K2
C3K [--], P, A, T5 [=2 + 1 + 1 + 1]
C4Q0
C5-0
C6A, U{ff}3 [=1 + 2]
......

<tbody>
</tbody>


Thanks a lot for your help.
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
I don't know if you actually need VBA, but you can do that with just a formula:

ABCDEFG
1PersonRankCompanyPersonsCombined Rank
2A1C1A(), B (..)3
3B2C2K2
4K2C3K [--], P, A, T5
5P1C4Q0
6U2C5-0
7T1C6A, U{ff}3

<tbody>
</tbody>
Sheet7

Array Formulas
CellFormula
F2{=SUM(IF(ISNUMBER(FIND($A$2:$A$7,E2)),$B$2:$B$7))}

<tbody>
</tbody>
Entered with Ctrl+Shift+Enter. If entered correctly, Excel will surround with curly braces {}.
Note: Do not try and enter the {} manually yourself

<tbody>
</tbody>



Let us know if this works, or if you need VBA.
 
Last edited:
Upvote 0
Try this:-
"Person" Table starts "A2"
"Company" Table starts "D2"
Results in column "F".
Code:
[COLOR="Navy"]Sub[/COLOR] MG16Aug19
[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] num [COLOR="Navy"]As[/COLOR] Double
[COLOR="Navy"]Set[/COLOR] Rng = Range("A2", Range("A" & Rows.Count).End(xlUp))
[COLOR="Navy"]With[/COLOR] CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
   .Item(Dn.Value) = Dn.Offset(, 1)
[COLOR="Navy"]Next[/COLOR]
[COLOR="Navy"]Set[/COLOR] Rng = Range("E2", Range("E" & Rows.Count).End(xlUp))
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    [COLOR="Navy"]For[/COLOR] n = 1 To Len(Dn.Value)
        [COLOR="Navy"]If[/COLOR] Dn.Characters(n, 1).Text >= Chr(65) And Dn.Characters(n, 1).Text <= Chr(90) [COLOR="Navy"]Then[/COLOR]
            num = num + .Item(Dn.Characters(n, 1).Text)
        [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]Next[/COLOR] n
Dn.Offset(, 1).Value = num: num = 0
[COLOR="Navy"]Next[/COLOR] Dn
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]With[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Hi Mick,

Thanks for that. The names are actual names and not just "A","B,"C", etc.

How do you change the code for that?

Thanks a lot for your help!
 
Upvote 0
Try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG18Aug50
im Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] num [COLOR="Navy"]As[/COLOR] Double, sp [COLOR="Navy"]As[/COLOR] Variant, nstr [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]Set[/COLOR] Rng = Range("A2", Range("A" & Rows.Count).End(xlUp))
[COLOR="Navy"]With[/COLOR] CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
   .Item(Dn.Value) = Dn.Offset(, 1)
[COLOR="Navy"]Next[/COLOR]
[COLOR="Navy"]Set[/COLOR] Rng = Range("E2", Range("E" & Rows.Count).End(xlUp))
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    [COLOR="Navy"]For[/COLOR] n = 1 To Len(Dn.Value)
        [COLOR="Navy"]If[/COLOR] UCase(Dn.Characters(n, 1).Text) >= Chr(65) And UCase(Dn.Characters(n, 1).Text) <= Chr(90) [COLOR="Navy"]Then[/COLOR]
            nstr = nstr & Dn.Characters(n, 1).Text
        [COLOR="Navy"]Else[/COLOR]
         nstr = nstr & ","
        [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]Next[/COLOR] n
    sp = Split(nstr, ",")
    [COLOR="Navy"]For[/COLOR] n = 0 To UBound(sp)
       [COLOR="Navy"]If[/COLOR] .exists(sp(n)) [COLOR="Navy"]Then[/COLOR]
        num = num + .Item(sp(n))
        [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]Next[/COLOR] n
Dn.Offset(, 1).Value = num: num = 0: nstr = ""
[COLOR="Navy"]Next[/COLOR] Dn
[COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0

Forum statistics

Threads
1,214,611
Messages
6,120,510
Members
448,967
Latest member
screechyboy79

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