2013-07-17 313 views
1

我的代碼的這一行給我:錯誤13:類型不匹配 如何在「或」中組合「nd」statent? 例如:如果A或B或C或(d和e)或(f和g)使用「或」語句嵌套「和」語句

下面是代碼

If (cell.Value) = "FTV1" _ 
     Or (cell.Value) = "FTV2" _ 
     Or (cell.Value) = "FTV3" _ 
     Or (cell.Value) = "FTV4" _ 
     Or (cell.Value) = "FTV5" _ 
     Or (cell.Value) = "ISTCR" _ 
     Or (cell.Value) = "CAST" _ 
     Or (cell.Value) = "Rig" _ 
     Or (cell.Value) > 50000 And (cell.Value) < 52000 _ 
     Or (cell.Value) > 55000 And (cell.Value) < 56000 Then 

預先感謝

+0

使用括號('('')')包圍複合詞陳述 –

+1

爲什麼所有'(cell.Value)'而不是簡單的'cell.Value' ??? –

+1

看起來好像使用Select Case語句會更好。 .. –

回答

0

括在額外括號中的複合語句解釋編譯器如何排序邏輯statments ...

試試這個:

If (cell.Value) = "FTV1" _ 
     Or (cell.Value) = "FTV2" _ 
     Or (cell.Value) = "FTV3" _ 
     Or (cell.Value) = "FTV4" _ 
     Or (cell.Value) = "FTV5" _ 
     Or (cell.Value) = "ISTCR" _ 
     Or (cell.Value) = "CAST" _ 
     Or (cell.Value) = "Rig" _ 
     Or ((cell.Value) > 50000 And (cell.Value) < 52000) _ 
     Or ((cell.Value) > 55000 And (cell.Value) < 56000) Then 
+0

沒有這個工作 – user2385809

+0

假設細胞是一個範圍,當它不起作用時它的值是多少? –

+0

當這是錯誤的?任何其他隨機的東西。 – user2385809

0

And條件有點棘手,但不要在此語句中嵌套另一個Select CaseIf/Then,但假定您要處理字符串或整數/長整型值,則應該可以工作(測試)。

Select Case cell.Value 
    Case "FTV2", "FTV3", "FTV4", "FTV5", _ 
     "ISTCR", "CAST", "Rig", _ 
     50001 To 51999, 55001 To 55999 

     'Do your code or call subroutine/function, here. 

    Case Else 

     'Do other code, or, do nothing, if the above criteria not met. 

End Select 

如果您正在使用雙/十進制數據類型的工作,那麼我可能需要修改的東西像(未經測試):

Select Case cell.Value 
    Case "FTV2", "FTV3", "FTV4", "FTV5", _ 
     "ISTCR", "CAST", "Rig", _ 
     50000 to 56000 

     If (Not IsNumeric(cell.Value)) Or _ 
      (50000 < cell.Value < 52000) Or _ 
      (55000 < cell.Value < 56000) Then 

      'Do your code or call subroutine/function, here. 

     End If 
    Case Else 

     'Do other code, or, do nothing, if the above criteria not met. 

End Select