+ Reply to Thread
Results 1 to 13 of 13

List the 20 most common names in a column

  1. #1
    Forum Contributor
    Join Date
    05-09-2013
    Location
    london
    MS-Off Ver
    Excel 2016
    Posts
    107

    List the 20 most common names in a column

    Dear all,

    I'm having difficulties trying to solve one problem.

    I have a spreadsheet with B column with more than 2000 different street names (B1 to B2000). I need to have a VBA code that can count the top 20 rank of the most repeated street names and list them on G1 to G20 cells.

    Does anyone can help me?

  2. #2
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: List the 20 most common names in a column

    It would help immensely if you could provide a sample file. Knowing the file structure is just as important as knowing the logic requirement. See yellow block above that describes how to upload a file. Make sure to desensitize it.

  3. #3
    Forum Contributor
    Join Date
    05-09-2013
    Location
    london
    MS-Off Ver
    Excel 2016
    Posts
    107

    Re: List the 20 most common names in a column

    Dear maniacb

    Here I attach a sample file. This is just a sample because the original file has more than 2000 entries. So what I would like to have is a top 20 of the most repeated street names.

    Thank you so much
    Attached Files Attached Files

  4. #4
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,888

    Re: List the 20 most common names in a column

    Here is a power query solution. I see in your profile that you are using XL2007. Is this still the case. If not, please update your profile so that contributors can offer solutions that match your version. I have offered a version that will only work with XL2010 or later. I did not notice your version until after I had built a solution.

    Please Login or Register  to view this content.
    Power Query is a free AddIn for Excel 2010 and 2013, and is built-in functionality from Excel 2016 onwards (where it is referred to as "Get & Transform Data").

    It is a powerful yet simple way of getting, changing and using data from a broad variety of sources, creating steps which may be easily repeated and refreshed. I strongly recommend learning how to use Power Query - it's among the most powerful functionalities of Excel.

    - Follow this link to learn how to install Power Query in Excel 2010 / 2013.

    - Follow this link for an introduction to Power Query functionality.

    - Follow this link for a video which demonstrates how to use Power Query code provided.
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  5. #5
    Forum Contributor
    Join Date
    05-09-2013
    Location
    london
    MS-Off Ver
    Excel 2016
    Posts
    107

    Re: List the 20 most common names in a column

    Dear alansidman,

    Thank you so much for your time helping me.

    I do indeed have Excel 2016, I will update my profile information.

    I've tried your code but I got this error message:

    " Expression.Error: Couldn't find a Excel table with the name "Table1".
    Details:
    Table1 "
    I shall say that my Excel software is in Portuguese, I have translated the error message that I got to english.

    Thank you

  6. #6
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,888

    Re: List the 20 most common names in a column

    the range of data that you bring into PQ Editor needs to be named Table1 or you need to change the first line of Mcode to the Table Name you have.

  7. #7
    Forum Contributor
    Join Date
    05-09-2013
    Location
    london
    MS-Off Ver
    Excel 2016
    Posts
    107

    Re: List the 20 most common names in a column

    By "range of data that needs to be named Table1" do you mean the spreadsheet name of the Excel file?

  8. #8
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,888

    Re: List the 20 most common names in a column

    No the range of data to be imported to PQ Editor

    See the attached file.
    Attached Files Attached Files

  9. #9
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: List the 20 most common names in a column

    Greetings to all.

    The attached model is -almost- the same as the one posted by @alansidman.

    The important difference is that it tries to "dodge" Power Query's big problem: changing data column titles.
    Indeed: a good development can be frustrated if we change -for example- 'Street Name' to 'Street NAME'.
    Attached Files Attached Files
    You are always very welcome if you add reputation by clicking the * (bottom left) of each message that has helped you.

  10. #10
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: List the 20 most common names in a column

    Just in case you still want a VBA solution, here is one way to do it:

    Please Login or Register  to view this content.
    Last edited by maniacb; 03-29-2023 at 09:51 PM.

  11. #11
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,466

    Re: List the 20 most common names in a column

    One way using UDF
    In G2 and drag down 40 rows (or as far as you need):

    =top($B$2:$B$36,ROWS($1:1))

    with B2:B36 is street name range, rows() generate 1,2,3...

    and code of that function: (Alt-F11 to open VBA window, insert/module, copy this code into)
    PHP Code: 
    Option Explicit
    Function Top(ByVal rng As RangeAs Integer)
    Dim i&, j&, k&, namedic As Objectres(1 To 100001 To 2), tmp1tmp2
    Set dic 
    CreateObject("Scripting.Dictionary")
    name rng.Value
    For 1 To UBound(name)
        If 
    Not dic.exists(name(i1)) Then
            dic
    .Add name(i1), 100
            k 
    1res(k1) = name(i1): res(k2) = 100
        
    Else
            
    dic(name(i1)) = dic(name(i1)) + 1
            
    For 1 To k
                
    If res(j1) = name(i1Then res(j2) = res(j2) + 1
            Next
        End 
    If
    Next
    For 1 To k 1
        
    For 1 To k
            
    If res(i2) < res(j2Then
                tmp1 
    res(j1): tmp2 res(j2)
                
    res(j1) = res(i1): res(j2) = res(i2)
                
    res(i1) = tmp1res(i2) = tmp2
            End 
    If
        
    Next
    Next
    Top 
    IIf(<= dic.Countres(n1), "")
    End Function 
    Attached Files Attached Files
    Last edited by bebo021999; 03-29-2023 at 11:22 PM.
    Quang PT

  12. #12
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: List the 20 most common names in a column

    Greetings to all. A VBA solution would approach it like this:

    Please Login or Register  to view this content.
    Attached Files Attached Files

  13. #13
    Forum Contributor
    Join Date
    05-09-2013
    Location
    london
    MS-Off Ver
    Excel 2016
    Posts
    107

    Re: List the 20 most common names in a column

    Dear alansidman, beyond excel, maniacb and bebo021999,

    Thank you all so much for the help that all of you guys gave to me.

    I now can sort what I really want! Thank you thank you thank you!!

+ 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. [SOLVED] Lookup names then return names from another column to create another list, sorted.
    By major_johnson in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 11-07-2022, 03:10 PM
  2. Column has Excused Names, Column B has List of Names, Can Column C Cross Reference B v A?
    By knuckledownlacrosse in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 01-27-2022, 07:13 PM
  3. How many names are common between lists and how many are not in any list
    By Clairep in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 09-09-2021, 05:39 AM
  4. [SOLVED] Need help deriving a list of common values in a column
    By cozcoester in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 02-06-2016, 01:14 AM
  5. Replies: 2
    Last Post: 12-08-2015, 03:01 PM
  6. List only the common term(s) from a list in a column
    By jordan2322 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-06-2013, 07:50 PM
  7. Replies: 5
    Last Post: 04-17-2012, 12:28 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