VBA

时间:2016-05-05 17:56:04

标签: excel vba excel-vba if-statement

我试图在VBA中互相写两个If语句,但它给了我错误的答案。当我调试它时,它表明程序没有通过ElseIf并继续使用第一个If的else。

如何让两个If语句在彼此之间正常工作? 代码:

If ConfigBox.Value <> ... Then

     If ListBox1.Value = ... Then

          DO  SOMETHING
    Else
          DO  SOMETHING
    End If

ElseIf ListBox1.Value = ... Or ListBox1.Value = ... Then

    DO  SOMETHING
Else

    DO  SOMETHING
End If

2 个答案:

答案 0 :(得分:1)

你的筑巢有点偏。你可能想做这样的事情:

If ConfigBox.Value <> ... Then

    If ListBox1.Value = ... Then
          Code
    ElseIF ListBox1.Value = ... Or ListBox1.Value = ... Then
          Code
    Else
        Code 'if ListBox1.Value doesn't meet above criteria
    End If

Else
    Code 'if ConfigBox criteria is not met.  You could start another nested If for the ListBox1 here too.

End If

答案 1 :(得分:0)

尝试使用以下

If ((ConfigBox.Value <> "1") And (ListBox1.Value = "2")) Then
    'Do Something
ElseIf ((ConfigBox.Value <> "1") And (ListBox1.Value <> "2")) Then
    'Do Something
ElseIf ListBox1.Value = "3" Or ListBox1.Value = "4" Then
    'Do Something
Else
    'Do Something
End If