Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding a character 2

Status
Not open for further replies.

OzzieGeorge

Programmer
Jan 14, 2005
2,611
AU
Is there a command which returns the position of a character or start of a sub string within a string? I have previously looped my way through the string looking for what I wanted but I am looking for more efficient ways of doing things.
 
I know you can test if it is there with instr but how do you find it's position?
 
Me, I'd check the help file.

Here's one of the things it says:

"The InStr function returns the position of the first occurrence of one string within another
 

The line

Position = instr("Please pay attention to the answers", "pay")

returns 8 for Position.

The Missinglinq

Richmond, Virginia

There's ALWAYS more than one way to skin a cat!
 
Amazing

I hadn't read the line that way, I thought it only returned a yes or no to whether it was there or not. Thanks to all for putting me right. I only dabble in this area and it is a revelation to me everytime you guys help me out Thanks again.
 
I thought it only returned a yes or no to whether it was there or not. "

That is why it helps to actually read Help. There is no mention of InStr being Boolean (yes/no).

faq219-2884

Gerry
My paintings and sculpture
 
And yet the line

If InStr(1, cleanstr, "ERROR", vbTextCompare) Then cleanstr = "Idle" 'Else cleanstr = Right(cleanstr, (Len(cleanstr) - (pos + 4)))

works. Which is why I believed it to be boolean. But you are of course right, I was working late when I first decided to use instr and just went with it. Thanks for pointing out the error
 
As you are probably now aware, that works because in VBA 0 is FALSE and TRUE is anything that isn't FALSE
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top