+ Reply to Thread
Results 1 to 5 of 5

count number of instances of a text string in a cell

  1. #1
    Registered User
    Join Date
    11-28-2008
    Location
    Sydney, Australia
    Posts
    9

    count number of instances of a text string in a cell

    hi - does anyone know best way for vba macro or worksheet function to count the number of instances of a seach string within a cell for example if a cell contains following

    Ghanimeh & Culbert P & MA

    is there a simple way to count the number of instances of "&" symbol ie answer being 2!

    thx lawrie

  2. #2
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Lawrie

    Something like

    Please Login or Register  to view this content.
    This will work for a single character. If you have a string that is more than 1 char long, then divide the result by the length of the char string.

    rylo

  3. #3
    Valued Forum Contributor
    Join Date
    11-11-2008
    Location
    Euro
    MS-Off Ver
    2007, 2010
    Posts
    470
    Quote Originally Posted by lawrencef View Post
    hi - does anyone know best way for vba macro or worksheet function to count the number of instances of a seach string within a cell for example if a cell contains following

    Ghanimeh & Culbert P & MA

    is there a simple way to count the number of instances of "&" symbol ie answer being 2!

    thx lawrie
    To see the link: http://www.excelforum.com/excel-work...ml#post2004429

  4. #4
    Valued Forum Contributor Richard Schollar's Avatar
    Join Date
    05-23-2006
    Location
    Hampshire UK
    MS-Off Ver
    Excel 2002
    Posts
    1,264
    A possible VBA alternative would be:

    Please Login or Register  to view this content.
    Richard
    Richard Schollar
    Microsoft MVP - Excel

  5. #5
    Valued Forum Contributor
    Join Date
    09-19-2008
    Location
    It varies ...
    MS-Off Ver
    Office365 - 64bit
    Posts
    862
    Re- the use of:
    Please Login or Register  to view this content.
    ... nice lateral thinking .. I do like it

    I would offer this caution though to those not familiar with arrays - this will return the correct answer only if Option Base 1 is not set (Option Base 0 being the default) ... overwise, the answer is
    Please Login or Register  to view this content.
    .

    For those not familiar with using arrays, it's because UBound returns the index of the last element, and since Split separates out the substrings between delimiters, there is one more substring than there are delimeters ... so if the first substring is in array index 0 and the second in array index 1, then there must only be 1 delimeter (the "&" in this case) to create the 2 substrings and the "count" is the upper index of the array, which is 1. And this will also work if the & is the last character, because the last element is still populated with a zero length string.

    Hope that helps.
    Last edited by VBA Noob; 11-28-2008 at 02:10 PM. Reason: added code tags
    MatrixMan.
    --------------------------------------
    If this - or any - reply helps you, remember to say thanks by clicking on *Add Reputation.
    If your issue is now resolved, remember to mark as solved - click Thread Tools at top right of thread.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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