2017-07-14 4 views
0

下のコードで "OR"を正しく使用しましたか?誰かが私を助けてくれますか?VBAでif in文を使用する方法

If Cells(i, 3).Value = "BRITISH TELECOM" Or "CHRISTIES INTERNATIO" Or "DTAG" Or "IMAGINE COMMUNICATIONS CORP" Then 

答えて

8

いいえ、あなたはしませんでした:

If Cells(i, 3).Value = "BRITISH TELECOM" Or _ 
    Cells(i, 3).Value = "CHRISTIES INTERNATIO" Or _ 
    Cells(i, 3).Value = "DTAG" Or _ 
    Cells(i, 3).Value = "IMAGINE COMMUNICATIONS CORP" Then 

代替はSelect Caseステートメントを使用することであろう。あなたがテストに多くの条件を持っている場合、これらが特に有用である:

Select Case Cells(i, 3).Value 
    Case "BRITISH TELECOM", _ 
     "CHRISTIES INTERNATIO", _ 
     "DTAG", _ 
     "IMAGINE COMMUNICATIONS CORP" 

     'Do something 

    Case "Some other string", _ 
     "and another string" 

     'Do something else 

    Case Else 

     'Do something if none of the other statements evaluated to True 

End Select 

Select Case文は以下のIf文と同じであろうと:

If Cells(i, 3).Value = "BRITISH TELECOM" Or _ 
    Cells(i, 3).Value = "CHRISTIES INTERNATIO" Or _ 
    Cells(i, 3).Value = "DTAG" Or _ 
    Cells(i, 3).Value = "IMAGINE COMMUNICATIONS CORP" Then 

     'Do something 

ElseIf Cells(i, 3).Value = "Some other string" Or _ 
     Cells(i, 3).Value = "and another string" Then 

     'Do something else 

Else 

     'Do something if none of the other statements evaluated to True 

End If 

実際の問題とは無関係なが、中コメント内の他の質問への回答:

彼らは文字列と比較することはできませんので、最初にエラーをテストする必要があります。例えば

:私は選択のケースで私の答えを投稿する必要がある場合

If IsError(Cells(i, 3).Value) Then 

    'Do whatever you want to do with error values such as #N/A 

ElseIf Cells(i, 3).Value = "BRITISH TELECOM" Or _ 
    Cells(i, 3).Value = "CHRISTIES INTERNATIO" Or _ 
    Cells(i, 3).Value = "DTAG" Or _ 
    Cells(i, 3).Value = "IMAGINE COMMUNICATIONS CORP" Then 

    '... 

または

If IsError(Cells(i, 3).Value) Then 

    'Do whatever you want to do with error values such as #N/A 

Else  

    Select Case Cells(i, 3).Value 
     Case "BRITISH TELECOM", _ 
      "CHRISTIES INTERNATIO", _ 
      "DTAG", _ 
      "IMAGINE COMMUNICATIONS CORP" 

      'Do something 

     Case "Some other string", _ 
      "and another string" 

      'Do something else 

     Case Else 

      'Do something if none of the other statements evaluated to True 

    End Select 

End If 
+1

今、私は考えています?いいえ、私はあなたがそれを閉じさせるよ); –

+1

@ShaiRadoハッピー? – YowE3K

+0

ありがとう、私は型不一致エラーを取得しています(ランタイムエラー '13') – Jagadeesh

関連する問題