|
-
Jan 5th, 2006, 12:20 AM
#1
Thread Starter
New Member
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.
-
Jan 5th, 2006, 09:13 AM
#2
Lively Member
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
-
Jan 5th, 2006, 08:04 PM
#3
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|