+ Reply to Thread
Results 1 to 5 of 5

VBA to List each count of instances of a given value in another column

  1. #1
    Registered User
    Join Date
    07-17-2013
    Location
    Portland, OR
    MS-Off Ver
    Excel 207
    Posts
    14

    VBA to List each count of instances of a given value in another column

    Hello,

    I've used advanced filtering to give me the unique values in column F and have those values displayed in column M using the code below.

    Please Login or Register  to view this content.
    I would now like to use VBA to display a "countif" for the number of instances each value that's in column M appears in column F. I know that's kind of an awkward sentence but I couldn't think of a better way to word it.

    Example:

    Column F Column M Column N
    Jon Jon 2
    Jon Sally 3
    Sally Jen 1
    Sally Mike 2
    Sally Sam 2
    Jen
    Mike
    Mike
    Sam
    Sam


    Any ideas? Thanks for your help!

  2. #2
    Valued Forum Contributor AlvaroSiza's Avatar
    Join Date
    09-19-2007
    Location
    Staffordshire
    MS-Off Ver
    2007
    Posts
    591

    Re: VBA to List each count of instances of a given value in another column

    You could do it through arrays or collections, but this should be a bit easier to follow:

    Please Login or Register  to view this content.
    Perhaps it was the Noid who should have avoided me...
    If you are satisfied with my solution click the small star icon on the left. Thanks
    1. Make a copy of your workbook and run the following code on your copy (just in case)
    2. With excel open, press ALT+F11 to open the Visual Basic Editor (VBE). From the "Insert" menu, select "Module".
    3. Paste the code from above into the empty white space. Close the VBE.
    4. From the developer tab, choose "Macros", select the Sub Name, and click "Run".

  3. #3
    Registered User
    Join Date
    07-17-2013
    Location
    Portland, OR
    MS-Off Ver
    Excel 207
    Posts
    14

    Re: VBA to List each count of instances of a given value in another column

    Alvaro, that was perfect. Thank you for your help. I don't suppose you could give me a break down of what these sections do and how they do it? I'm still new to using VBA but I'm finding it very useful.

    Please Login or Register  to view this content.

  4. #4
    Valued Forum Contributor AlvaroSiza's Avatar
    Join Date
    09-19-2007
    Location
    Staffordshire
    MS-Off Ver
    2007
    Posts
    591

    Re: VBA to List each count of instances of a given value in another column

    Sure, no problem at all.

    Referring to first block, I'm simply telling excel via the Set syntax of the Range object, what to refer to later in the code. In this case, Column M represents the result of your 'unique' filter. End(xlDown) simply replicates hitting Shift+Down on your keyboard when within the application itself. So long as there are no blanks, it is a way to define a range by selecting the first cell and letting Excel find the last. I then repeated this for the "F" column values. We now have two ranges to work with.

    The second piece basically says, for each cell in the range containing only my unique names ("rngUnique" via Set rngUnique described above), perform the function countif, with the unique name as argument and the complete, raw, non-filtered range as the range to be queried. Assign the result of the countif to a Long variable. Then, in the cell one to the right of the unique name (Column N), place the value contained in the Long variable.

    I suppose this 2nd piece could be streamlined to:
    Please Login or Register  to view this content.
    which would lessen the code by one line and the use of one variable, which if not to reused over and over again, is likely the better practice.

    Hope this helps,
    Last edited by AlvaroSiza; 08-07-2013 at 04:41 PM.

  5. #5
    Registered User
    Join Date
    07-17-2013
    Location
    Portland, OR
    MS-Off Ver
    Excel 207
    Posts
    14

    Re: VBA to List each count of instances of a given value in another column

    Okay, awesome. Thanks so much for the detailed explanation. That helps a lot.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Search a list and count unique instances and match
    By eeanil in forum Excel General
    Replies: 2
    Last Post: 04-24-2012, 12:36 AM
  2. Count number of instances of a value in a column
    By Nate Westcott in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-09-2012, 05:35 PM
  3. Count instances in column
    By peter.nichols@m in forum Excel General
    Replies: 3
    Last Post: 03-15-2010, 11:20 AM
  4. is it possible to count instances of a word in a column?
    By alexfleming in forum Excel - New Users/Basics
    Replies: 1
    Last Post: 10-14-2009, 04:24 PM
  5. [SOLVED] How do I count the instances of numbers in a list?
    By John@NGC in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-18-2006, 04:00 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1