I have the following INSTR condition in Excel VBA which doesn’t work (all the time)
STR_TEXT="SKU1234 $100/10' $200/20'" ' < example string
IF INSTR(STR_TEXT,"/10'") AND INSTR(STR_TEXT,"/20'") THEN
' code
ELSE
' code
END IF
For some obscure reason, it seems like it cannot check for both conditions so the first IF, even if both condition match, doesn’t seem to work and goes to ELSE.
The following does work:
STR_TEXT="SKU1234 $100/10' $200/20'" ' < example string
IF INSTR(STR_TEXT,"/10'") THEN
IF INSTR(STR_TEXT,"/20'") THEN
' code
END IF
ELSE
' code
END IF
As you can see, if I separate the conditions on the first IF, it works.
But I would prefer to have both conditions in same IF, as code is ‘cleaner’.
Anyone knows why and/or how to fix it without having to put an IF inside another IF ?
EXMAPLE:
What Tim is saying is that the instr function returns the position in the string of the first instance of the string being searched for..
so in your example: 13 is being returned for instr(str_Text,”/10′).
When VBA reads your version instr(str_text,”/10;”) (without the >0) then it sees that the result is not 1 (which means true) so it always hits the else)