【问题标题】:How to use OR in if statement in VBA [duplicate]如何在 VBA 中的 if 语句中使用 OR [重复]
【发布时间】:2017-07-14 09:36:46
【问题描述】:

我在下面的代码中是否正确使用了“OR”。有人可以帮帮我吗?

If Cells(i, 3).Value = "BRI TELECOM" Or "CHR INTERNATIO" Or "AG" Or "IMAG COMMUNICATIONS CORP" Then

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    不,你没有:

    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
    

    与实际问题无关,但回应 cmets 中的进一步问题:

    如果您的数据中有错误值,它们将无法与字符串进行比较,因此您需要先测试错误。

    例如:

    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
    

    【讨论】:

    • 现在我在考虑是否应该使用 Select Case 发布我的答案???不,我会让你关闭它;)
    • @ShaiRado 快乐吗?
    • 谢谢,我收到类型不匹配错误(运行时错误'13':)
    • @YowE3K 非常,现在我可以小睡一下了 ;)
    • C列与行i相交的单元格中的值是字符串值吗?还是可能是 #N/A#DIV/0! 之类的错误值?
    猜你喜欢
    • 1970-01-01
    • 2022-10-01
    • 1970-01-01
    • 2014-09-30
    • 2015-05-16
    • 2011-02-21
    • 1970-01-01
    • 2010-09-13
    • 1970-01-01
    相关资源
    最近更新 更多