布尔和If语句

我有一个代码将一个单元格数组转换成一个循环的string。 如果单元格包含特定的string,它会正确地将布尔variables放入布尔发现的variables中,但是用这些variables作为目标的if语句将被跳过,并且只会执行最后一个else语句。 我不知道为什么会这样,并会喜欢任何input,为什么if语句可能不会看到variables为true。 提前致谢。

trimmedValX = Trim$(ur(r, m(6))) *creates the string Found1 = InStr(trimmedValX, "still sold") *checks to see if string is in the created string, if it is variable is set as True Found2 = InStr(trimmedValX, "Still sold") Found3 = InStr(trimmedValX, "Discontinued") If Found3 = True Then cell1.Interior.Color = rColor ElseIf Found1 = True or Found2 = True Then cell1.Interior.Color = gColor Else cell1.Interior.Color = mColor End If 

我会build议一些没有InStr的东西

  trimmedValX = Trim$(ur(r, m(6))) With cell1.Interior If UCase(trimmedValX) Like "*DISCONTINUED*" Then .Color = rColor ElseIf UCase(trimmedValX) Like "*STILL SOLD*" Then .Color = gColor Else .Color = mColor End If End With 

InStr不返回一个布尔值,而是返回子string的起始位置。 尝试使用此代替。 InStr参考

 trimmedValX = Trim$(ur(r, m(6))) *creates the string Found1 = InStr(trimmedValX, "still sold") *checks to see if string is in the created string, if it is variable is set as True Found2 = InStr(trimmedValX, "Still sold") Found3 = InStr(trimmedValX, "Discontinued") If Found3 > 0 Then cell1.Interior.Color = rColor ElseIf Found1 > 0 or Found2 > 0Then cell1.Interior.Color = gColor Else cell1.Interior.Color = mColor End If