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.
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:
Function TextInCell(rng As Range, txt As String) As Boolean
Dim found As Boolean
found = False
If InStr(1, rng.Value, txt) > 0 Then found = True
TextInCell = found
End Function
Sub Usage()
If TextInCell(Range("A27"), "foobar") Then
MsgBox "Found"
Else
MsgBox "Not Found"
End If
End Sub
Re: Determining if string exists within cell (Excel)
Thanks, that's exactly what I needed!