Need Excel Formula to calculate number of group members previously worked with (includes diff. size groups over time)

ea2146

New Member
Joined
Apr 9, 2012
Messages
30
Hello,

Thank you very much in advance for any help you can provide. I REALLY appreciate it!

I am looking for a formula that will calculate the values that currently appear in column D below. Specifically, I need the formula to calculate the total number of current group members with whom each student previously worked. For example, there is a value of "3" next to Carol's name in group 5 because Carol had previously worked with Susan, Eric, and Ashley at different times in the past.

If anyone could help me create this formula I would be extremely grateful!!


DateGroupStudentNumber of Current Group Members Worked with Previously
2/10/20151Susan0
2/10/20151Kayley0
2/10/20151Carol0
5/15/20152Kayley1
5/15/20152Carol1
5/15/20152Jeff0
5/15/20152Eric0
5/15/20152Clarissa0
5/15/20152Ashley0
7/15/20153Susan2
7/15/20153Kayley3
7/15/20153Carol3
7/15/20153Jeff2
8/18/20154Alice0
8/18/20154Drew0
8/18/20154James0
8/18/20154Carol0
8/18/20154John0
9/22/20155Susan1
9/22/20155Eric2
9/22/20155Ashley2
9/22/20155Carol3
9/22/20155Frank0
9/22/20155David0
9/22/20155Hannah0
9/22/20155Thomas0

<tbody>
</tbody>
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Very tricky. I couldn't figure out a way to do it with native Excel functions, so I wrote a UDF. Even that was tricky. But here it is and it seems to work fine.

1) Open your workbook
2) Press Alt-F11 to open the VBA editor
3) From the menu, select Insert --> module
4) On the window that opens up, paste this code:
Code:
Public Function PrevStudents(Grp As Range, Stdnt As Range)
Dim MyGroups(1000) As String
Dim r As Long, wk, i As Long, j As Long, ctr As Integer

    Erase MyGroups
    ctr = 0
    r = 2
    
    While Cells(r, Grp.Column) <> ""
        grpno = CInt(Cells(r, Grp.Column))
        MyGroups(grpno) = MyGroups(grpno) & "," & Cells(r, Stdnt.Column)
        r = r + 1
    Wend
    
    wk = Split(MyGroups(CInt(Grp.Value)), ",")
    For i = 1 To UBound(wk)
        For j = 1 To Grp - 1
            If InStr(MyGroups(j), wk(i)) > 0 And InStr(MyGroups(j), Stdnt.Value) > 0 Then
                ctr = ctr + 1
                GoTo NextI:
            End If
        Next j
NextI:
    Next i
    
    PrevStudents = IIf(ctr > 0, ctr - 1, 0)
            
End Function
5) Return to your Excel sheet.
6) In D2, enter this function:
=PrevStudents(B2,C2)
7) Copy cell D2 and paste it down the column.

Let me know how that works.
 
Upvote 0
Thank you SO MUCH, Eric W!!!!

I just tried this and it works perfectly! This is brilliant. I really appreciate the help!
 
Upvote 0
Hi again,

Quick question - I presented an example that was analogous, but not identical to the data I am working with. The script you provided works perfectly for the example I provided, but I receive the "#VALUE!" error when I try to run it using my actual data which looks like this (see below - I have tens of thousands of rows). I couldn't figure out what to alter in the script to make it work. Any suggestions? Thanks!!!

bcdateexpid_CODEName
6/26/19211Alexander Frederick RichmondWollaston1875UK
6/26/19211Alexander MacMillanHeron1884UK
6/26/19211Alexander MitchellKellas1868UK
6/26/19211Ang PasangSherpaNepal
6/26/19211Charles KennethHoward-Bury1883UK
6/26/19211Edward OliverWheeler1890Canada
6/26/19211GarongSherpaNepal
6/26/19211George Herbert LeighMallory1886UK
6/26/19211Guy HenryBullock1887UK
6/26/19211Harold AndrewRaeburn1865UK
6/26/19211Henry TreiseMorshead1882UK
6/26/19211LagaySherpaNepal
6/26/19211UnknownIndia
5/1/19222Arthur WilliamWakefield1876UK
5/1/19222Charles GranvilleBruce1866UK
5/1/19222Charles JohnMorris1895UK
5/1/19222ChettanSherpaNepal
5/1/19222Colin Grant (Ferdie)Crawford1890UK
5/1/19222DorjeSherpaNepal
5/1/19222Edward FelixNorton1884UK
5/1/19222Edward Lisle (Bill)Strutt1874UK
5/1/19222George Herbert LeighMallory1886UK
5/1/19222George IngleFinch1888Australia
5/1/19222Henry TreiseMorshead1882UK
5/1/19222John Baptist LuciusNoel1890UK
5/1/19222John GeoffreyBruce1896UK
5/1/19222LhakpaSherpaNepal
5/1/19222NorbuSherpaNepal
5/1/19222PasangSherpaNepal
5/1/19222PemaSherpaNepal
5/1/19222SangeSherpaNepal
5/1/19222TejbirBuraNepal
5/1/19222TembaSherpaNepal
5/1/19222Theodore HowardSomervell1890UK
5/1/19222Thomas GeorgeLongstaff1875UK
4/28/19243Andrew Comyn (Sandy)Irvine1902UK
4/28/19243BentleyBeetham1886UK
4/28/19243Edward FelixNorton1884UK
4/28/19243Edward OswaldShebbeare1884UK
4/28/19243George Herbert LeighMallory1886UK
4/28/19243John Baptist LuciusNoel1890UK
4/28/19243John de VereHazard1885UK
4/28/19243John GeoffreyBruce1896UK
4/28/19243Man BahadurNepal
4/28/19243Noel EwartOdell1890UK
4/28/19243Richard William GeorgeHingston1887UK
4/28/19243ShamsherPunNepal
4/28/19243Theodore HowardSomervell1890UK

<colgroup><col style="mso-width-source:userset;mso-width-alt:2742;width:56pt" width="75"> <col style="mso-width-source:userset;mso-width-alt:3510;width:72pt" width="96"> <col style="mso-width-source:userset;mso-width-alt:13165;width:270pt" width="360"> </colgroup><tbody>
</tbody>
 
Upvote 0
I just copied the latest sample set, and it worked fine. My best guess is that you're using different columns. If the above data is in Columns A-C, then
=PrevStudents(B2,C2)

is OK. The first value (B2) should be the group column, and the second value (C2) should be the name. If your data is in columns B-D, then you should change the formula to:
=PrevStudents(C2,D2)

When I get the column references off, I get the #VALUE error too. Try that, and let me know if it works.
 
Upvote 0
Thank you again!!! I got it to work by changing

"Dim MyGroups(1000) As String"

to
"Dim MyGroups(10000) As String"

I assume the number in parentheses here refers to the max number of groups? The reason I think I was getting the error is because I have more than 1000 groups in my data which is why I changed that value to 10000.

Thanks for writing the UDF - it's really helpful :)
 
Upvote 0
Good catch! I meant to mention that, but you caught it.

Glad you got it working.
 
Upvote 0

Forum statistics

Threads
1,216,099
Messages
6,128,813
Members
449,469
Latest member
Kingwi11y

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