English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I am tryng to find out if the string "EndStamp" is in the string "ts" this is the code I am using:

If InStr(-1, ts, EndStamp, vbBinaryCompare) > -1 Then

when the code runs I get this message:

run time error '5':
invalid procedure call or argument

I have used this funcion before and it has always worked. Do you know what I am doing wrong?

2007-02-05 11:35:32 · 4 answers · asked by James R 2 in Computers & Internet Programming & Design

4 answers

if instr(ts,EndStamp)>0 then
...action
end if

-------
Instr returns position in string if found, 0 if not. Don't need all the frills.

2007-02-05 11:41:48 · answer #1 · answered by steve.c_50 6 · 1 0

Vb6 Instr Function

2016-12-14 18:34:07 · answer #2 · answered by pariasca 4 · 0 0

The -1 is why you get the error. InStr begins its index at 1, not -1.

Also, the function returns false whenever a needle is not found in the haystack, and a positive integer which is evaluated as true if the needle is found, so you don't need the comparison in your If statement.

If InStr(ts, EndStamp) Then
'code to execute if found
Else
'code to execute if not found
End If

UPDATE:

Gene is simply wrong. The error this questioner received is "invalid procedure call or argument."

The invalid argument is the first argument, which is out of range. Had Gene taken 10 seconds to look at the MSDN documentation for the InStr function, he would have seen that any time you use a starting index less than 1, you get exception code 5:

http://msdn2.microsoft.com/en-us/library/8460tsh1.aspx

I have over 10 years' experience with programming. I actually read documentation. Please, don't insult me again.

2007-02-05 12:11:06 · answer #3 · answered by Anonymous · 0 1

Your runtime error has nothing to do with the way you wrote your code, although other posters have made valid suggestions.

Go to "Tools, References" in the VBA IDE. It sounds like you've lost your reference to a library, which can cause any VBA code module to get confused. The thing it thinks it can't find is the VBA Engine and the "InStr" function itself. Fix the broken reference. If none are missing, then go to the "Debug" menu and choose "Compile." Sometimes the IDE "forgets" that the reference is there. That's a bug with VBA.

2007-02-05 12:17:08 · answer #4 · answered by Gene 3 · 0 2

fedest.com, questions and answers