+ Reply to Thread
Results 1 to 10 of 10

Counting number of occurance of substring in col of data.

  1. #1
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Counting number of occurance of substring in col of data.

    Hi,

    I have a situation i have not encountered before and it is causing me some headache as to which way i should solve this problem.

    I have two lists..........ListA is a column of String values, ListB is also a column of string values. Normally to compare lists i would use a dictionary however when doing thisI am comparing listB to listA to see if EXACT values from B appear in A.

    However, what i have now is ListA is a long string and what i am trying to do is see if ListB strings "appear" as a subset in listA. For example.
    Suppose we have the following:
    ListA (1)= Dave was a man and he was strong his number is 1445 and he has nothing else.
    ListB(1) = 1445

    So when doing the comparison i want to count the number of times 1445 appears as a substring in the listA items.

    Any thoughts on how to do this?

  2. #2
    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,882

    Re: Counting number of occurance of substring in col of data.

    Here is an approach I might try:

    Create a VBA loop

    Please Login or Register  to view this content.
    Then Sum the range
    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

  3. #3
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Counting number of occurance of substring in col of data.

    Hi welchs,

    I'd do this problem with a wildcard search from each string in List B into List A. I'd use this kind of formula.
    =COUNTIF($A$2:$A$100,"*" & C2 & "*")

    See the attached and no VBA needed.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  4. #4
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Counting number of occurance of substring in col of data.

    Thanks for the info. I think i forgot to mention that all of this has to be done via vba..........i guess i could use vba to insert a formula but i would prefer to stay away from this if possible.

    My listA is about 5 to 8,000 rows and listB is about 5000 rows...........so this is why i was hoping to use some sort of dictionary thingy to help same time...........but as it stands now i would have to compare 1st item in listB to all items in listA, then compare 2nd item in listB to all of items in listA......and so on.........this is going to take a while.........but i am not sure of a better way to do it.

    any thoughts?

  5. #5
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Counting number of occurance of substring in col of data.

    Unless you have some sort of delimiter or separator for lists in one column, you would not be able to use a dictionary.
    A dictionary can only work with exact match, so the 1445 has to be separated from the other data so as to get a match in the other column.
    You can use an array loop which can speed up the execution.

  6. #6
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Counting number of occurance of substring in col of data.

    An array loop.........do you mean just two loops where one loop is inside another? Or did you mean something else?

  7. #7
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Counting number of occurance of substring in col of data.

    Hi,

    Here is the VBA that does the same thing as the Non-VBA solution. See if it works for you.
    Please Login or Register  to view this content.
    Look at the Dynamic Named Range of ListB and ListA also. You can change this to a For Each loop and it might work faster. Code and example in the attached.

  8. #8
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Counting number of occurance of substring in col of data.

    as an option (based on the MarvinP's sample file)
    Please Login or Register  to view this content.

  9. #9
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Counting number of occurance of substring in col of data.

    i am going to have to look at both those code examples a little closer....thanks.

  10. #10
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Counting number of occurance of substring in col of data.

    I meant the same as Nilem's adjusted code. Looping through an array is faster than looping through a range. Remember a dictionary is sometimes referred to as an Associated array.

+ 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] Counting the number of occurance with multiple criteria in excel
    By Redbull58 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 06-27-2015, 02:26 PM
  2. Find nth number of occurance of data in vlookup
    By rajansettu in forum Excel General
    Replies: 3
    Last Post: 09-08-2014, 09:13 AM
  3. count the occurance of a number in a column, within data parameters.
    By Shermin in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-25-2013, 06:02 AM
  4. Counting the Occurance of a Root Number only
    By iamconstance in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 02-14-2013, 06:50 PM
  5. [SOLVED] Extract Substring, then Ignore that Substring, while collecting data from Other substrings
    By Sameki121 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-17-2012, 05:21 PM
  6. Replies: 0
    Last Post: 08-25-2005, 05:48 AM
  7. Replies: 0
    Last Post: 08-25-2005, 05:44 AM

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