2017-09-05 114 views
0

我有一個代碼,將一個單元格數組轉換爲一個循環的字符串。如果單元格包含特定的字符串,它會正確地將布爾變量放入布爾發現的變量中,但用這些變量作爲目標的if語句會被跳過,並且只會執行最後一個else語句。我不知道爲什麼會這樣並且會喜歡關於爲什麼if語句可能不會將變量視爲true的任何輸入。提前致謝。布爾和If語句

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 

回答

0

我會不惜一切

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 
+0

提出好的建議InStr函數內部消除感謝你,得到了代碼爲我工作的需要。 – FrenchP

2

InStr不返回布爾值,而是返回子串的起始位置。嘗試使用此代替。 InStr Reference

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