使用if语句时移动到下一个单元格

时间:2015-07-15 19:48:59

标签: excel vba if-statement

我使用VBA非常新。这就是我所做的:

Dim ADT As String
    Dim Speed As String
    Dim SpaceAvailable As Integer
    Dim MinSpaceNMT As Integer
    Dim UserADT As Integer
    Dim UserSpeed  As Integer


    Sheet1.Activate
    Worksheets(1).Range("C9").Value = spnBridgeWidth.Value
    Worksheets(1).Range("C10").Value = spnVehLnWidth.Value
    Worksheets(1).Range("C12").Value = spnNoOfLn.Value

    Sheet1.Activate
    Worksheets(1).Range("G9").Value = spnBridgeWidth.Value
    Worksheets(1).Range("G10").Value = spnVehLnWidth.Value
    Worksheets(1).Range("G12").Value = spnNoOfLn.Value

    Sheet1.Activate
    Worksheets(1).Range("K9").Value = spnBridgeWidth.Value
    Worksheets(1).Range("K10").Value = spnVehLnWidth.Value
    Worksheets(1).Range("K12").Value = spnNoOfLn.Value

    Sheet1.Activate
    Worksheets(1).Range("O9").Value = spnBridgeWidth.Value
    Worksheets(1).Range("O10").Value = spnVehLnWidth.Value
    Worksheets(1).Range("O12").Value = spnNoOfLn.Value

    Sheet1.Activate
    Range("C4").Select

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

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

    UserADT = UserTool.txtADT.Value
    UserSpeed = UserTool.txtSpeed.Value


    If UserADT < ADT And UserSpeed < Speed Then

    If SpaceAvailable > MinSpaceNMT Then


   MsgBox "Safe passage can be within the bridge with Alt. 1"

   Else
            Sheet1.Activate
            Range("G4").Select

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

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

            UserADT = UserTool.txtADT.Value
            UserSpeed = UserTool.txtSpeed.Value


   If UserADT >= ADT And UserSpeed >= Speed Then

   If SpaceAvailable > MinSpaceNMT Then


            MsgBox "Safe passage can be within the bridge with Alt. 1.2"

   Else

            MsgBox "Safe passage can be outside the bridge"


              End If
           End If
       End If
   End If

我正在尝试让程序移动到包含信息的下一列,并检查是否满足“If语句”。但是,在“其他”之后它没有做到它应该做的事情。请帮帮我!

2 个答案:

答案 0 :(得分:1)

这可以充分整理你的代码,为循环的结果添加一些逻辑。

Sub tble()     Dim co As Long

    With Sheet1
        .Range("C9, G9, K9, O9").Value = spnBridgeWidth.Value
        .Range("C10, G10, K10, O10").Value = spnVehLnWidth.Value
        .Range("C12, G12, K12, O12").Value = spnNoOfLn.Value

        UserADT = UserTool.txtADT.Value
        UserSpeed = UserTool.txtSpeed.Value

        For co = 0 To 12 Step 4
            With .Range("C4").Offset(0, co)
                ADT = .Value
                Speed = .Offset(1, 0).Value 'could also be .Range("C5")
                SpaceAvailable = .Offset(10, 0).Value
                MinSpaceNMT = .Offset(14, 0).Value
            End With

            'At this point I am unclear on why the logic changes
            'from less than to greater than or equal

            'If UserADT < ADT And UserSpeed < Speed Then
            '    If SpaceAvailable > MinSpaceNMT Then

            'If UserADT >= ADT And UserSpeed >= Speed Then
            '    If SpaceAvailable > MinSpaceNMT Then
            '        MsgBox "Safe passage can be within the bridge with Alt. 1.2"
            '    Else
            '        MsgBox "Safe passage can be outside the bridge"
            'When you have cycled through and met your conditions, use:
            Exit For
            'and you will jump out of the loop
        Next co
    End With
End Sub

后一部分循环通过C9,G9,K9,O9,每次通过时向柱子加4。您只需检查每个传递的值,看它们是否符合突破的标准。

有关远离依赖选择和激活以实现目标的更多方法,请参阅How to avoid using Select in Excel VBA macros

答案 1 :(得分:0)

如果没有更多信息,以下内容可以帮助您入门。注意我删除了.Select和.Activate,因为它们不是必需的,它使代码更容易阅读:

Sub test()
Dim spnBridgeWidth, spnVenLnWidth, spnNoOfLn
Dim startCell     As Range, otherCell As Range

Set startCell = Range("C4")
Set otherCell = Range("G4")
spnBridgeWidth = 10
spnVenLnWidth = 20
spnNoOfLn = 40


With Sheet1
    .Range("C9").Value = spnBridgeWidth
    .Range("C10").Value = spnVehLnWidth
    .Range("C12").Value = spnNoOfLn

    .Range("G9").Value = spnBridgeWidth
    .Range("G10").Value = spnVehLnWidth
    .Range("G12").Value = spnNoOfLn

    Sheet1.Activate
    .Range("K9").Value = spnBridgeWidth
    .Range("K10").Value = spnVehLnWidth
    .Range("K12").Value = spnNoOfLn

    .Range("O9").Value = spnBridgeWidth
    .Range("O10").Value = spnVehLnWidth
    .Range("O12").Value = spnNoOfLn
End With

With startCell
    ADT = .Value
    Speed = .Offset(1, 0).Value

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

UserADT = UserTool.txtADT.Value
UserSpeed = UserTool.txtSpeed.Value


If UserADT < ADT And UserSpeed < Speed Then

    If SpaceAvailable > MinSpaceNMT Then


        MsgBox "Safe passage can be within the bridge with Alt. 1"

    Else
       ' Sheet1.Activate
        'Range("G4").Select

        ADT = otherCell.Value
        Speed = otherCell.Offset(1, 0).Value

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

        UserADT = UserTool.txtADT.Value
        UserSpeed = UserTool.txtSpeed.Value


        If UserADT >= ADT And UserSpeed >= Speed Then

            If SpaceAvailable > MinSpaceNMT Then


                MsgBox "Safe passage can be within the bridge with Alt. 1.2"

            Else

                MsgBox "Safe passage can be outside the bridge"


            End If
        End If
    End If
End If
End Sub

编辑:Bah - Jeeped打败了我,他的外表多了一点&#34;放在一起&#34; :P