Find a string into another string

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I would like to know if a String belongs to another string.

for example :
Dim KeyString as String
Dim Sentence as String
Dim Result as Interger

Result = worksheetFunction.Find(KeyString,Sentence)
if (Result<>"") then
Msgbox "KeyString exists within Senetence"
EndIf

but it doesn't work.
i just want to check if my KeyString belongs to Sentence, that's all.
basically i store into Sentence the value of a cell.
thx

Maileen
 
Try using the instr function. It returns the location of the beginning of one
text string withing another. If the string is not found it returns zero.

HTH
 
thanks a lot,
it works great. :)

Maileen

Jim said:
Try using the instr function. It returns the location of the beginning of one
text string withing another. If the string is not found it returns zero.

HTH

:
 
WorksheetFunction is an application object.

Try:

Sub test()

Dim KeyString As String

Dim Sentence As String

Dim Result As String

KeyString = "AAAA"

Sentence = "BBBB CCCC AAAA NNNN"


Result = Application.WorksheetFunction.Find(KeyString, Sentance)

If (Result <> "") Then

MsgBox "KeyString exists within Senetence"

End If

End Sub

Cheers,

Allan P. London, CPA
 
Your code will work if the string is found, but if not, it raises an error.
Instr as suggested by Jim Thomlinson would seem a better choice.
 
Back
Top