InStr() as If - condition seems not to work

D

dominik lenné

Hi.

Office 2000. Excel VBA.

The following "if" structure didn't work:


If InStr(Zeile, ":") And Not InStr(Zeile, "h:") Then '
statement
End If


"Not work" means, that "statement" was executed if there was ":" present, regardless of wether "h:" was present in the line or not.

The following workaround worked:


boolHColon = False
If InStr(strZeile, "h:") Then
hcolon = True
End If
If InStr(strZeile, ":") And Not boolHColon Then '
statement
End If


Seemingly, the InStr() is not evaluated correctly in the "if" conditions. Is that true? Or what else?

regards

Dominik Lenné
 
J

Jezebel

The problem is that Instr() is not a boolean function. It returns a long. So
your function works if you use ---

If InStr(Zeile, ":") > 0 And InStr(Zeile, "h:") = 0 Then '




Hi.

Office 2000. Excel VBA.

The following "if" structure didn't work:


If InStr(Zeile, ":") And Not InStr(Zeile, "h:") Then '
statement
End If


"Not work" means, that "statement" was executed if there was ":" present,
regardless of wether "h:" was present in the line or not.

The following workaround worked:


boolHColon = False
If InStr(strZeile, "h:") Then
hcolon = True
End If
If InStr(strZeile, ":") And Not boolHColon Then '
statement
End If


Seemingly, the InStr() is not evaluated correctly in the "if" conditions. Is
that true? Or what else?

regards

Dominik Lenné
 
J

Jean-Guy Marcil

dominik lenné was telling us:
dominik lenné nous racontait que :
Hi.

Office 2000. Excel VBA.

The following "if" structure didn't work:


If InStr(Zeile, ":") And Not InStr(Zeile, "h:") Then '
statement
End If


"Not work" means, that "statement" was executed if there was ":"
present, regardless of wether "h:" was present in the line or not.

The following workaround worked:


boolHColon = False
If InStr(strZeile, "h:") Then
hcolon = True
End If
If InStr(strZeile, ":") And Not boolHColon Then '
statement
End If


Seemingly, the InStr() is not evaluated correctly in the "if"
conditions. Is that true? Or what else?

InStr works perfectly well within an IF statement.
You seem to have forgotten :) that it reruns the position in which the
search criteria is found within = the string, or ) if it is not fond. It
does not return true or false.

Try with this code:

'_______________________________________
Dim Zeile As String

Zeile = "my Testh: Hello"
'Zeile = "my Test: Hello"
'Zeile = "my Test Hello"

If InStr(Zeile, ":") > 0 And InStr(Zeile, "h:") = 0 Then
MsgBox "There is a colon, but no h colon"
Else
MsgBox "There is a h colon or no colon at all"
End If
'_______________________________________

By the way, if you have more questions, do not forget that this is a Word
group... not an Excel one...

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
D

dominik lenné

Thanks to both of you. it would have cost me 1 to 2 hrs to find it on my own, i suppose.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top