vba-需要满足三个条件才能执行的案例陈述

时间:2015-07-17 17:09:13

标签: excel vba case-statement

我正在尝试让我的程序只在满足所有三个条件时执行Case语句。但是,即使没有满足第三个,它仍在执行。我该怎么解决这个问题?这是代码:

'Scenario 1
Sheet2.Activate 
Range("C4").Select 

ADT = ActiveCell.Value 
Speed = ActiveCell.Offset(1, 0).Value 
Nonmotorizedexpected = ActiveCell.Offset(2, 0).Value 

SpaceAvailable = ActiveCell.Offset(10, 0).Value 
MinSpaceNMT = ActiveCell.Offset(14, 0).Value 

UserADT = UserTool.txtADT.Value 
UserSpeed = UserTool.txtSpeed.Value 
UserNonmotorizedexpected = Worksheets(7).Range("D3").Value 



 Select Case True 
   Case ((UserADT <= ADT) And (UserSpeed < Speed) And UserNonmotorizedexpected =     Worksheets(7).Range("D3").Value) 
    Select Case True 
    Case SpaceAvailable > MinSpaceNMT 
        MsgBox "Safe passage can be within the bridge with Alt. 2.1" 
    Case Else 
        MsgBox "Check next alt." 
    End Select 
   End Select 


'Scenario 5
Sheet2.Activate 
Range("S4").Select 

ADT = ActiveCell.Value 
Speed = ActiveCell.Offset(1, 0).Value 
Nonmotorizedexpected = ActiveCell.Offset(2, 0).Value 

SpaceAvailable = ActiveCell.Offset(10, 0).Value 
MinSpaceNMT = ActiveCell.Offset(14, 0).Value 

UserNonmotorizedexpected = Worksheets(7).Range("D4").Value 

Select Case True 
Case ((UserADT <= ADT) And (UserSpeed < Speed) And (UserNonmotorizedexpected = Worksheets(7).Range("D4").Value)) 
Select Case True 
Case SpaceAvailable > MinSpaceNMT 
    MsgBox "Safe passage can be within the bridge with Alt. 2.5" 
Case Else 
    MsgBox "Check next alt." 
End Select 

结束选择

第三个条件是UserNonmotorizedexpected。它与方案1和5不同。但是,由于前两个条件相同,因此将两个方案作为解决方案。我希望程序只采用满足所有三个条件的程序。

这是评论后代码的样子:

'Scenario 3
    Sheet2.Activate
    Range("K4").Select

    ADT = ActiveCell.Value
    Speed = ActiveCell.Offset(1, 0).Value
    Nonmotorizedexpected = ActiveCell.Offset(2, 0).Value

    SpaceAvailable = ActiveCell.Offset(10, 0).Value
    MinSpaceNMT = ActiveCell.Offset(14, 0).Value

    UserADT = UserTool.txtADT.Value
    UserSpeed = UserTool.txtSpeed.Value
    UserNonmotorizedexpected = Worksheets(7).Range("D3").Value

     Select Case True
            Case ((UserADT <= ADT) And (UserSpeed >= Speed) And UserNonmotorizedexpected = Nonmotorizedexpected)
                Select Case True
                    Case SpaceAvailable > MinSpaceNMT
                        MsgBox "Safe passage can be within the bridge with Alt. 2.3"
                    Case Else
                        MsgBox "Check next alt."
                End Select
        End Select

2 个答案:

答案 0 :(得分:0)

我不确切地知道你想要完成什么,但是你的Case语句似乎是尝试创建和If ... Then..ElseIf结构。这是你应该做的更好的结构(如果你的代码是我认为的那样):

 .....
 UserNonmotorizedexpected = Worksheets(7).Range("D3").Value 

 If ((UserADT <= ADT) And (UserSpeed < Speed) _
        And UserNonmotorizedexpected = Worksheets(7).Range("D3").Value) Then
      If SpaceAvailable > MinSpaceNMT Then
           MsgBox "Safe passage can be within the bridge with Alt. 2.1" 
      Else 
           MsgBox "Check next alt." 
      End If
 End If

      Sheet2.Activate 
      Range("S4").Select 

      ADT = ActiveCell.Value 
      Speed = ActiveCell.Offset(1, 0).Value 
      Nonmotorizedexpected = ActiveCell.Offset(2, 0).Value 

     SpaceAvailable = ActiveCell.Offset(10, 0).Value 
     MinSpaceNMT = ActiveCell.Offset(14, 0).Value 

     UserNonmotorizedexpected = Worksheets(7).Range("D4").Value
     If ((UserADT <= ADT) And (UserSpeed < Speed) _
           And (UserNonmotorizedexpected = Worksheets(7).Range("D4").Value)) Then
         If SpaceAvailable > MinSpaceNMT Then
                  MsgBox "Safe passage can be within the bridge with Alt. 2.5" 
         Else 
                  MsgBox "Check next alt." 
        End If
     End If

然而,您遇到的真正问题是您在一行中设置:

 UserNonmotorizedexpected = Worksheets(7).Range("D3").Value 

然后你要检查几行:

 If.....And (UserNonmotorizedexpected = Worksheets(7).Range("D3").Value

当然这些都是平等的!您只需将变量设置为此值!

也许你的意思是:

 If.....And (UserNonmotorizedexpeced = Nonmotorizedexpected)

希望这有帮助。

答案 1 :(得分:0)

我怀疑你的变量不包含你认为在测试这三个条件时所做的值。没有你的工作簿,就不可能为你检查这个,但是......

考虑一下:

Select Case True
    Case 1 <= 2 And 2 < 3 And 3 = 3
        Beep
End Select

以上是您逻辑的简化表示。当然,执行时会发出一声嘟嘟声。

但这里没有听到哔声:

Select Case True
    Case 1 <= 2 And 2 < 3 And 3 = 4
        Beep
End Select

...因为没有达到第3个条件。

请重新检查您的变量及其类型。