Determining if string exists within cell (Excel)-VBForums
Results 1 to 3 of 3

Thread: Determining if string exists within cell (Excel)

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2005
    Posts
    8

    Determining if string exists within cell (Excel)

    OK, I'm a beginning VB.NET programmer and I recently started a small VBA project. My main obstacle is this one little bit of code I need to write.

    What I need to do is determine (true or false) whether a certain text string exists within a certain cell. Let's say I want to know if the word "foobar" was in cell A27. I want a statement that will return true (so I can use it as part of a conditional) if the text of A27 were, say, "omGFoobARLOLHAHA."

    How would I do that? I did a bunch of experimenting today with the Find function, but that doesn't quite seem to be what I'm looking for.

    Thanks much.

  2. #2
    Lively Member
    Join Date
    Jun 2005
    Posts
    112

    Re: Determining if string exists within cell (Excel)

    You could just use the InStr() function to find text in a particular cell.

    If you really want a function to return a boolean value, then this will do it for you

    VB Code:
    1. Function TextInCell(rng As Range, txt As String) As Boolean
    2.     Dim found As Boolean
    3.     found = False
    4.     If InStr(1, rng.Value, txt) > 0 Then found = True
    5.     TextInCell = found
    6. End Function
    7.  
    8. Sub Usage()
    9.     If TextInCell(Range("A27"), "foobar") Then
    10.         MsgBox "Found"
    11.     Else
    12.         MsgBox "Not Found"
    13.     End If
    14. End Sub

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2005
    Posts
    8

    Re: Determining if string exists within cell (Excel)

    Thanks, that's exactly what I needed!

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.