Instr条件不适用于2个或更多条件 [英] Instr condition doesn't work with 2 or more conditions

查看:145
本文介绍了Instr条件不适用于2个或更多条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Excel VBA中具有以下INSTR条件,不能正常工作(所有时间)

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

对于一些模糊的原因,似乎无法检查这两种情况第一个IF,即使这两个条件匹配,似乎都不起作用,并且进行到ELSE。

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.

以下功能正常工作:

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

正如你所见如果我分开了第一个IF的条件,它是有效的。
但是我希望两个条件都是同一个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'.

任何人都知道为什么和/或如何解决它,而不需要将IF放在另一个IF中?

Anyone knows why and/or how to fix it without having to put an IF inside another IF ?

推荐答案

EXMAPLE:

if instr(str_Text,"/10'") > 0 AND instr(str_text,"/20'") > 0 then

Tim说的是,instr函数返回第一个实例的字符串中的位置的字符串被搜索..

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..

所以在你的例子中:13被返回instr(str_Text,/ 10)。

so in your example: 13 is being returned for instr(str_Text,"/10').

当VBA读取您的版本instr(str_text,/ 10;)(没有> 0)时,它会看到结果不是1(这意味着true),所以它总是命中其他)

这篇关于Instr条件不适用于2个或更多条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆