两个If语句在VBA中相互内部

我试图在VBA中相互编写两个If语句,但它给了我错误的答案。 当我debugging它时,它显示程序没有通过ElseIf并继续与第一个If的其他。

我怎么能有两个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 

你的嵌套是有点closures。 你会想要做这样的事情:

 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 

尝试下面

 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