Excel VBA: Count distinct entries

What VBA would I use so that it counts number of distinct records between column A, B and C and assigns that number to a variable (i.e. x)

i.e. Here is some sample data...
A                          B                       C
Last Name      First Name           Class
Coyote               Wiley                  Physics
Coyote               Wiley                  Purchasing
Runner               Road                  Track
Runner               Road                  pyrotechnics (for smoke trail)
Runner               Road                  Bottle English

i.e. According to the data above, If I used VBA to find out number of classes that Road Runner was taking, the VBA would say (x=3) because Road Runner is taking 3 classes. (What VBA would I use to do this?)
ouestqueAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cyberkiwiCommented:
This should get you started
Sub CDistinct()
Dim r As Range
Dim c As Range
Dim myvariable
Dim t
Set r = Intersect(ActiveSheet.Columns(1), ActiveSheet.UsedRange.Rows.EntireRow)
Set r = r.Resize(r.Rows.Count - 1).Offset(1)  ' skip first row (headers)
For Each c In r
t = -1
On Error Resume Next
t = Application.WorksheetFunction.Match(c.Value, r.Resize(c.Row - r.Row), 0)
On Error GoTo 0
If t = -1 Then
    myvariable = c.Value & " appears " & Application.WorksheetFunction.CountIf(r, c.Value) & " times"
    MsgBox myvariable
End If
Next
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Patrick MatthewsCommented:
ouestque,

When you say "distinct", I assume that you mean distinct across all three columns.  For example, with this sample data:


Last Name      First Name      Class
Coyote      Wiley      Physics
Coyote      Wiley      Purchasing
Runner      Road      Track
Runner      Road      pyrotechnics (for smoke trail)
Runner      Road      Bottle English
Bunny      Bugs      Wabbit season
Bunny      Bugs      Wabbit season
Coyote      Wiley      underwater basket weaving
Coyote      Winona      underwater basket weaving


Wiley Coyote has 3 distinct records, Road Runner has three, Bugs Bunny has 1 distinct record (notice that although he has two records overall, they duplicate the class, and Winona Coyote has 1 distinct record.  (Note that Wiley Coyote and Winona Coyote are separate entities :)

So, assuming you really want the number of distinct classes for each "person", I did the following:

1) In D1 I put the heading Concat, and in D2 the formula =A2&"|"&B2&"|"&C2.  I copied that formula down as needed to the end of the data

2) In E1 I put the heading Counter, and in E2 the formula =1/COUNTIF(D:D,D2).  I likewise copied that down as needed

3) I then created a PivotTable with Last Name and First Name as row fields, and (sum of) Counter as the data field.  That sum is really the count of distinct entries for each "person".


@cyberkiwi: when I run your code against my sample data set above I get the following messages:
'Coyote appears 4 times'
'Runner appears 3 times'
'Bunny appears 2 times'

Patrick
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Office

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.