Results 1 to 6 of 6

Thread: InStr() within a Range, mistmatch error

  1. #1
    Board Regular
    Join Date
    May 2005
    Posts
    686
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default InStr() within a Range, mistmatch error

    i have a range of rows that contains text. I want to check if a string exists within this range. but i keep getting errors

    what is proper syntax?

    Rng = Range("a1:a200")

    if Instr(Rng, "All Formulas") then
    if Instr(Rng.text, "All Formulas") then
    if Instr(Rng.value, "All Formulas") then

    none work. if i try to print the range i get

    Debug.Print (Rng.Text) = Null
    Debug.Print (Rng.Value) = type Mismatch

  2. #2
    Board Regular
    Join Date
    Sep 2004
    Posts
    1,377
    Post Thanks / Like
    Mentioned
    9 Post(s)
    Tagged
    0 Thread(s)

    Default Re: InStr() within a Range, mistmatch error

    Try
    Code:
    For Each c In Rng
    If Instr(c, "All Formulas") <> 0 then
     ' do what needs to be done
    End If
    Next c
    Last edited by jolivanes; Sep 21st, 2019 at 01:55 PM.
    Theory is when you know something, but it doesnít work. Practice is when something works, but you donít know why. Politicians combine theory and practice: nothing works and they donít know why.

  3. #3
    MrExcel MVP
    Moderator
    Fluff's Avatar
    Join Date
    Jun 2014
    Location
    Chippenham
    Posts
    27,997
    Post Thanks / Like
    Mentioned
    467 Post(s)
    Tagged
    47 Thread(s)

    Default Re: InStr() within a Range, mistmatch error

    If you just want to see if the string occurs anywhere in the range use
    Code:
    MsgBox IIf(InStr(1, Join(Application.Transpose(Range("A1:A200").Value), "|"), "All Formulas", vbTextCompare) > 0, "yes", "no")
    - Posting Data try one of these tools
    - Posting guidelines, forum rules and terms of use
    - Read the FAQs

    Running Office 365 on Win 10

  4. #4
    MrExcel MVP Rick Rothstein's Avatar
    Join Date
    Apr 2011
    Location
    New Jersey, USA
    Posts
    35,214
    Post Thanks / Like
    Mentioned
    92 Post(s)
    Tagged
    33 Thread(s)

    Default Re: InStr() within a Range, mistmatch error

    Quote Originally Posted by Fluff View Post
    If you just want to see if the string occurs anywhere in the range use
    Code:
    MsgBox IIf(InStr(1, Join(Application.Transpose(Range("A1:A200").Value), "|"), "All Formulas", vbTextCompare) > 0, "yes", "no")
    If that is what the OP wants, then here is another way...
    Code:
    MsgBox IIf(Application.CountIf(Range("A1:A200"), "*All Formulas*"), "Yes", "No")
    Rick's "mini" blog... http://www.excelfox.com/forum/f22/
    .
    Want to post a small screen shot? See Part B here.

  5. #5
    Board Regular
    Join Date
    May 2005
    Posts
    686
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: InStr() within a Range, mistmatch error

    Quote Originally Posted by Rick Rothstein View Post
    If that is what the OP wants, then here is another way...
    Code:
    MsgBox IIf(Application.CountIf(Range("A1:A200"), "*All Formulas*"), "Yes", "No")
    thanks, this seems to be the cleanest and easiest. why the * before and after? indicates wildcard, if text appears before and after??? thanks for the help

  6. #6
    MrExcel MVP Rick Rothstein's Avatar
    Join Date
    Apr 2011
    Location
    New Jersey, USA
    Posts
    35,214
    Post Thanks / Like
    Mentioned
    92 Post(s)
    Tagged
    33 Thread(s)

    Default Re: InStr() within a Range, mistmatch error

    Quote Originally Posted by ilcaa View Post
    why the * before and after? indicates wildcard, if text appears before and after???
    Yes, the asterisks allow for there to be text either before or after (or both) the text you are searching for. I did that because you tried to use InStr which allows for text to before or after the text being searched for. If you are looking for the text to be the only text in the cell, then remove the asterisks.
    Rick's "mini" blog... http://www.excelfox.com/forum/f22/
    .
    Want to post a small screen shot? See Part B here.

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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