+ Reply to Thread
Results 1 to 13 of 13

InStr Function

  1. #1
    Registered User
    Join Date
    09-19-2012
    Location
    Anaheim, California
    MS-Off Ver
    Excel 2010
    Posts
    22

    InStr Function

    I have been using a function similar to this to search within a larger string within a variable cell. From what I have seen, I am successfully able to count how many times "Apple" shows up within a larger string. How is this possible? From what I have seen about the InStr function it seems like I need to add a sub string. Any help or explanation is much appreciated.

    Please Login or Register  to view this content.
    Last edited by msolari; 12-18-2012 at 01:57 PM.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: InStr Function

    InStr will only return the position of the first match after the starting point.

    You can't really use it for counting the no of times a substring appears in a string.
    If posting code please use code tags, see here.

  3. #3
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: InStr Function

    To count the number of times a string appears in a larger string try:

    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    09-19-2012
    Location
    Anaheim, California
    MS-Off Ver
    Excel 2010
    Posts
    22

    Re: InStr Function

    Thanks for the reply! I understand where you are coming from. Why then though does the function above work so well. It will find "Apple" within a larger string satisfying the If statement thus adding to my array count. I like to understand why things work when they do

  5. #5
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: InStr Function

    Ah, right.

    Instr either returns a 0 if the sub-string is not found, or a positive integer, indicating its position, if it is.

    VBA interprets 0 as FALSE and any positive integer as TRUE, so the If statment works fine

  6. #6
    Registered User
    Join Date
    09-19-2012
    Location
    Anaheim, California
    MS-Off Ver
    Excel 2010
    Posts
    22

    Re: InStr Function

    Just to be clear, In my last comment I am referring to my original code. Not the code Andrew was so kind to provide.

  7. #7
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: InStr Function

    it depends what you want as the answer if 'apple' appears twice in the same cell?
    if it should count as 1 then instr is exactly the function you want, since it searches for the location of the match and returns 0 if there isn't one (which is equivalent to False in the If statement)
    if it should count as 2 then you need Marcol's function
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: InStr Function

    Your code will find Apple but it will only find it once.

  9. #9
    Registered User
    Join Date
    09-19-2012
    Location
    Anaheim, California
    MS-Off Ver
    Excel 2010
    Posts
    22

    Re: InStr Function

    Andrew you are quick to reply. Thanks for your help. One more question. So "Apple" in my code counted as the sub string? That's what is most confusing. From what I see about InStr the sub string would come after "Apple" in the original example. Here is an example of finding pp within Apple I think. If the sub string is separated by a comma then how does "Apple" in the original example become the sub string?

    Please Login or Register  to view this content.

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: InStr Function

    the syntax for InStr is this.

    InStr([Start], [String1], [String2], [Compare As VbCompareMethod = vbBinaryCompare])

    [Start] - the position you want to start looking from.

    [String1] - the string you want to look in.

    [String2] - the string you want to look for. ie the substring

    [Compare] - type of comparison to make.

    [Start] and [Compare] are optional.
    Last edited by Norie; 12-18-2012 at 01:39 PM.

  11. #11
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: InStr Function

    Because whatever is in Range("D" & i) is providing the main string and "apple" is the sub-string within that.

  12. #12
    Registered User
    Join Date
    09-19-2012
    Location
    Anaheim, California
    MS-Off Ver
    Excel 2010
    Posts
    22

    Re: InStr Function

    Ah thanks guys! Very helpful. So Range("D" & i) is not considered the [Start]. It is considered [String1]. That's what was hanging me up.

  13. #13
    Registered User
    Join Date
    09-19-2012
    Location
    Anaheim, California
    MS-Off Ver
    Excel 2010
    Posts
    22

    Re: InStr Function

    Oh and yes, I am only searching if "Apple" appears once. If I ever need to search for more than one instance I will use the code provided earlier. Thanks again. Marking as solved.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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