Counting names

%3CLINGO-SUB%20id%3D%22lingo-sub-2251235%22%20slang%3D%22en-US%22%3ECounting%20names%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2251235%22%20slang%3D%22en-US%22%3E%3CP%3EI%20am%20using%20a%20PC%20w%2FWindows%2010%20and%20MS%20365.%26nbsp%3B%20I%20have%20a%20workbook%20with%2012%20sheets%2C%20one%20for%20each%20month%20of%20the%20year.%26nbsp%3B%20On%20each%20worksheet%20I%20have%20a%20list%20of%20all%20the%20people%20who%20have%20attended%20a%20meeting.%26nbsp%3B%20Each%20line%20contains%20their%20Last%20Name%2C%20First%20Name%2C%20%26amp%3B%20ID%20Number.%20(Along%20with%20some%20other%20information%20that%20is%20not%20required%20for%20a%20final%20count).%26nbsp%3B%20What%20I%20need%20is%20a%20single%20worksheet%20at%20the%20end%20of%20each%20year%20that%20tells%20me%20how%20many%20times%20each%20individual%20has%20attended%20a%20meeting%20along%20with%20their%20name%20and%20ID.%26nbsp%3B%20Each%20individual%20could%20attend%20one%20or%20more%20meetings%20in%20any%20given%20year.%26nbsp%3B%20There%20is%20no%20master%20personnel%20list%20to%20work%20from%20since%20some%20individuals%20are%20transient.%26nbsp%3B%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2251235%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2251318%22%20slang%3D%22en-US%22%3ERe%3A%20Counting%20names%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2251318%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1015782%22%20target%3D%22_blank%22%3E%40ftb1ss%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHere%20is%20a%20macro%20that%20you%20can%20use.%20Change%20the%20constants%20at%20the%20beginning%20to%20match%20your%20setup.%3C%2FP%3E%0A%3CP%3EThe%20macro%20will%20create%20a%20summary%20sheet%20if%20it%20doesn't%20exist%2C%20otherwise%20it%20will%20overwrite%20its%20contents.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-visual%22%3E%3CCODE%3ESub%20ListNames()%0A%20%20%20%20'%20Change%20the%20constants%20as%20needed%0A%20%20%20%20Const%20strSummary%20%3D%20%22Summary%22%20'%20Name%20of%20the%20summary%20sheet%0A%20%20%20%20Const%20FNCol%20%3D%20%22A%22%20'%20First%20Name%20column%0A%20%20%20%20Const%20LNCol%20%3D%20%22B%22%20'%20Last%20Name%20column%0A%20%20%20%20Const%20IDCol%20%3D%20%22C%22%20'%20ID%20column%0A%20%20%20%20Dim%20dctFN%20As%20Object%0A%20%20%20%20Dim%20dctLN%20As%20Object%0A%20%20%20%20Dim%20dctID%20As%20Object%0A%20%20%20%20Dim%20wsh%20As%20Worksheet%0A%20%20%20%20Dim%20wss%20As%20Worksheet%0A%20%20%20%20Dim%20r%20As%20Long%0A%20%20%20%20Dim%20m%20As%20Long%0A%20%20%20%20Dim%20varID%20As%20Variant%0A%20%20%20%20Application.ScreenUpdating%20%3D%20False%0A%20%20%20%20Set%20dctFN%20%3D%20CreateObject(Class%3A%3D%22Scripting.Dictionary%22)%0A%20%20%20%20Set%20dctLN%20%3D%20CreateObject(Class%3A%3D%22Scripting.Dictionary%22)%0A%20%20%20%20Set%20dctID%20%3D%20CreateObject(Class%3A%3D%22Scripting.Dictionary%22)%0A%20%20%20%20On%20Error%20Resume%20Next%0A%20%20%20%20Set%20wss%20%3D%20Worksheets(strSummary)%0A%20%20%20%20On%20Error%20GoTo%200%0A%20%20%20%20If%20wss%20Is%20Nothing%20Then%0A%20%20%20%20%20%20%20%20Set%20wss%20%3D%20Worksheets.Add(After%3A%3DWorksheets(Worksheets.Count))%0A%20%20%20%20%20%20%20%20wss.Name%20%3D%20strSummary%0A%20%20%20%20Else%0A%20%20%20%20%20%20%20%20wss.Cells.Clear%0A%20%20%20%20End%20If%0A%20%20%20%20wss.Range(%22A1%3AD1%22).Value%20%3D%20Array(%22First%20Name%22%2C%20%22Last%20Name%22%2C%20%22ID%22%2C%20%22Count%22)%0A%20%20%20%20For%20Each%20wsh%20In%20Worksheets%0A%20%20%20%20%20%20%20%20If%20wsh.Name%20%26lt%3B%26gt%3B%20strSummary%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20m%20%3D%20wsh.Range(IDCol%20%26amp%3B%20wsh.Rows.Count).End(xlUp).Row%0A%20%20%20%20%20%20%20%20%20%20%20%20For%20r%20%3D%202%20To%20m%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20varID%20%3D%20wsh.Range(IDCol%20%26amp%3B%20r).Value%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20If%20dctID.Exists(varID)%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20dctID(varID)%20%3D%20dctID(varID)%20%2B%201%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Else%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20dctID.Add%20Key%3A%3DvarID%2C%20Item%3A%3D1%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20dctFN.Add%20Key%3A%3DvarID%2C%20Item%3A%3Dwsh.Range(FNCol%20%26amp%3B%20r).Value%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20dctLN.Add%20Key%3A%3DvarID%2C%20Item%3A%3Dwsh.Range(LNCol%20%26amp%3B%20r).Value%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20%20%20%20%20%20%20%20%20Next%20r%0A%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20Next%20wsh%0A%20%20%20%20wss.Range(%22A2%22).Resize(dctID.Count)%20%3D%20Application.Transpose(dctFN.Items)%0A%20%20%20%20wss.Range(%22B2%22).Resize(dctID.Count)%20%3D%20Application.Transpose(dctLN.Items)%0A%20%20%20%20wss.Range(%22C2%22).Resize(dctID.Count)%20%3D%20Application.Transpose(dctID.Keys)%0A%20%20%20%20wss.Range(%22D2%22).Resize(dctID.Count)%20%3D%20Application.Transpose(dctID.Items)%0A%20%20%20%20wss.Range(%22A1%3AD1%22).EntireColumn.AutoFit%0A%20%20%20%20Application.ScreenUpdating%20%3D%20True%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2253129%22%20slang%3D%22en-US%22%3ERe%3A%20Counting%20names%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2253129%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F127945%22%20target%3D%22_blank%22%3E%40Hans%20Vogelaar%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHans%2C%3C%2FP%3E%3CP%3EThanks%20for%20taking%20the%20time%20to%20give%20me%20a%20hand%20with%20this%20workbook.%26nbsp%3B%20I%20have%20never%20worked%20with%20macros%2C%20so%20I%20am%20struggling%20to%20fully%20understand%20what%20needs%20to%20be%20done.%26nbsp%3B%20I%20have%20made%20what%20I%20thought%20were%20the%20obvious%20changes%20to%20the%20macro%2C%20but%20when%20I%20run%20it%20I%20get%20nothing%20but%20an%20error%20message.%26nbsp%3B%20When%20I%20run%20the%20debug%20I%20get%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESub%20Work_Party_Total()%3CBR%20%2F%3E'%3CBR%20%2F%3E'%20Work_Party_Total%20Macro%3CBR%20%2F%3E'%3C%2FP%3E%3CP%3E'%3CBR%20%2F%3EChDir%20%22C%3A%5CUsers%5Cdwojm%5COneDrive%5CDocuments%22%3CBR%20%2F%3EActiveWorkbook.SaveAs%20Filename%3A%3D%20_%3CBR%20%2F%3E%22%3CA%20href%3D%22https%3A%2F%2Fd.docs.live.net%2F0b2e42162b4a9117%2FDocuments%2FAnnual%2520work%2520party%2520total.xlsm%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fd.docs.live.net%2F0b2e42162b4a9117%2FDocuments%2FAnnual%2520work%2520party%2520total.xlsm%3C%2FA%3E%22%20_%3CBR%20%2F%3E%2C%20FileFormat%3A%3DxlOpenXMLWorkbookMacroEnabled%2C%20CreateBackup%3A%3DFalse%3CBR%20%2F%3EActiveWindow.SmallScroll%20Down%3A%3D-45%3CBR%20%2F%3ESheets(%22Sheet2%22).Select%3CBR%20%2F%3EEnd%20Sub%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20attached%20a%20sample%20of%20what%20I%20want%20to%20use.%26nbsp%3B%20I%20just%20used%202%20months%20instead%20of%20all%2012.%26nbsp%3B%20If%20you%20would%20be%20kind%20enough%20to%20take%20a%20look%20and%20point%20me%20in%20the%20right%20direction%2C%20I%20would%20be%20grateful.%26nbsp%3B%20When%20this%20is%20done%20I%20am%20going%20to%20take%20some%20time%20and%20try%20my%20hand%20a%20a%20few%20macros.%26nbsp%3B%20I%20am%2078%20years%20old%20and%20have%20always%20been%20pretty%20basic%20with%20my%20excel%20files.%26nbsp%3B%20I%20guess%20it%20is%20time%20for%20me%20to%20exercise%20my%20brain%20a%20little.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%2C%3C%2FP%3E%3CP%3EDenny%20Olson%3C%2FP%3E%3C%2FLINGO-BODY%3E
New Contributor

I am using a PC w/Windows 10 and MS 365.  I have a workbook with 12 sheets, one for each month of the year.  On each worksheet I have a list of all the people who have attended a meeting.  Each line contains their Last Name, First Name, & ID Number. (Along with some other information that is not required for a final count).  What I need is a single worksheet at the end of each year that tells me how many times each individual has attended a meeting along with their name and ID.  Each individual could attend one or more meetings in any given year.  There is no master personnel list to work from since some individuals are transient.  

6 Replies

@ftb1ss 

Here is a macro that you can use. Change the constants at the beginning to match your setup.

The macro will create a summary sheet if it doesn't exist, otherwise it will overwrite its contents.

 

Sub ListNames()
    ' Change the constants as needed
    Const strSummary = "Summary" ' Name of the summary sheet
    Const FNCol = "A" ' First Name column
    Const LNCol = "B" ' Last Name column
    Const IDCol = "C" ' ID column
    Dim dctFN As Object
    Dim dctLN As Object
    Dim dctID As Object
    Dim wsh As Worksheet
    Dim wss As Worksheet
    Dim r As Long
    Dim m As Long
    Dim varID As Variant
    Application.ScreenUpdating = False
    Set dctFN = CreateObject(Class:="Scripting.Dictionary")
    Set dctLN = CreateObject(Class:="Scripting.Dictionary")
    Set dctID = CreateObject(Class:="Scripting.Dictionary")
    On Error Resume Next
    Set wss = Worksheets(strSummary)
    On Error GoTo 0
    If wss Is Nothing Then
        Set wss = Worksheets.Add(After:=Worksheets(Worksheets.Count))
        wss.Name = strSummary
    Else
        wss.Cells.Clear
    End If
    wss.Range("A1:D1").Value = Array("First Name", "Last Name", "ID", "Count")
    For Each wsh In Worksheets
        If wsh.Name <> strSummary Then
            m = wsh.Range(IDCol & wsh.Rows.Count).End(xlUp).Row
            For r = 2 To m
                varID = wsh.Range(IDCol & r).Value
                If dctID.Exists(varID) Then
                    dctID(varID) = dctID(varID) + 1
                Else
                    dctID.Add Key:=varID, Item:=1
                    dctFN.Add Key:=varID, Item:=wsh.Range(FNCol & r).Value
                    dctLN.Add Key:=varID, Item:=wsh.Range(LNCol & r).Value
                End If
            Next r
        End If
    Next wsh
    wss.Range("A2").Resize(dctID.Count) = Application.Transpose(dctFN.Items)
    wss.Range("B2").Resize(dctID.Count) = Application.Transpose(dctLN.Items)
    wss.Range("C2").Resize(dctID.Count) = Application.Transpose(dctID.Keys)
    wss.Range("D2").Resize(dctID.Count) = Application.Transpose(dctID.Items)
    wss.Range("A1:D1").EntireColumn.AutoFit
    Application.ScreenUpdating = True
End Sub

@Hans Vogelaar 

Hans,

Thanks for taking the time to give me a hand with this workbook.  I have never worked with macros, so I am struggling to fully understand what needs to be done.  I have made what I thought were the obvious changes to the macro, but when I run it I get nothing but an error message.  When I run the debug I get:

 

Sub Work_Party_Total()
'
' Work_Party_Total Macro
'

'
ChDir "C:\Users\dwojm\OneDrive\Documents"
ActiveWorkbook.SaveAs Filename:= _
"https://d.docs.live.net/0b2e42162b4a9117/Documents/Annual%20work%20party%20total.xlsm" _
, FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
ActiveWindow.SmallScroll Down:=-45
Sheets("Sheet2").Select
End Sub

 

I have attached a sample of what I want to use.  I just used 2 months instead of all 12.  If you would be kind enough to take a look and point me in the right direction, I would be grateful.  When this is done I am going to take some time and try my hand a a few macros.  I am 78 years old and have always been pretty basic with my excel files.  I guess it is time for me to exercise my brain a little.

 

Thanks,

Denny Olson

@ftb1ss 

The code should be copied into the code module in the Visual Basic Editor, not into a worksheet.

It requires a few changes: in your monthly sheets, first name is in column C, last name in column B, and NCRR is column D. You need to change the constants FNCol etc. accordingly.

And the data in those sheets begin in row 4, not - as I had assumed - in row 2.

See the attached version.

You can run the macro ListNames in either of the following ways:

  • In Excel, press Alt+F8 to activate the Macros dialog. Select ListNames, then click Run. Or:
  • Press Alt+F11 to activate the Visual Basic Editor, click anywhere in the code of the ListNames macro, and press F5 to run it. Switch back to Excel to view the result.

The macro that you mention won't work; among other things, it refers to a non-existent sheet Sheet2.

@ftb1ss As variant that could be done by Power Query.

- name range for each month

- add function which takes the month and clean the data

(month as text) =>
let
    Source = Excel.CurrentWorkbook(){[Name=month]}[Content],
    #"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
    #"Removed Other Columns" = Table.SelectColumns(#"Promoted Headers",{"Last", "First", "NCRR"}),
    #"Filtered Rows" = Table.SelectRows(#"Removed Other Columns", each ([Last] <> null))
in
    #"Filtered Rows"

- combine months

let
    Source = Table.Combine(
     {
        fnGetMonth("Mar"),
        fnGetMonth("Apr")
     }
    )
in
    Source

- count by NCPR

let
    Source = #"Combine months",
    #"Grouped Rows" = Table.Group(
        Source,
        {"NCRR"},
        {{"Count", each Table.RowCount(_), Int64.Type}}
    )
in
    #"Grouped Rows"

in general that could be all, but names are spelled differently in different months, thus we map Names on NCPR

- takes names and remove duplicated NCPR

let
    Source = #"Combine months",
    #"Removed Duplicates" = Table.Distinct(Source, {"NCRR"})
in
    #"Removed Duplicates"

- merge names with counts using NCPR as the key

let
    Source = Table.NestedJoin(
        Names, {"NCRR"},
        Counts, {"NCRR"},
        "Counts", JoinKind.LeftOuter
    ),
    #"Expanded Counts" = Table.ExpandTableColumn(
        Source, "Counts",
        {"Count"}, {"Count"}
    )
in
    #"Expanded Counts"

Now that's all.

image.png

@ftb1ss 

I was puzzled by the fact that I got different results from a worksheet formula.

image.png

Turns out I placed my trust in firstName/lastName combinations rather than the NCRR number.  Perhaps Paul Hollandsworth is known as Curtis Lenderman to his friends?  My formula was

= LET(
  distinct, UNIQUE(VSTACK(March,April)),
  first,  INDEX(distinct,,2),
  last, INDEX(distinct,,1),
  Mch, COUNTIFS(March[Last], last, March[First], first),
  Apr, COUNTIFS(April[Last], last, April[First], first),
  Mch+Apr)

I would need to write a Lambda function to replace Charles Williams's VSTACK for the formula to post to other machines and the method does not scale well to incorporate additional tables.

Once again, thanks for the assist. I will sit down with it next week and see if I can get it to run. I am wrapped up with grand kid for the next few days. They say you can't teach an old dog new tricks. You can, it just takes a little longer.

Denny Olson