如何基于If VBA在两个if语句之间进行选择?

时间:2018-12-07 08:37:12

标签: excel vba excel-vba

我想写一些代码,说明如果单元格的值= yes,则运行此“ IF”代码,如果不运行,则运行其他“ IF”代码。

这是我的潜艇:

Sub LTATradesTest()

Application.ScreenUpdating = False

Dim LastRow As Long, fs As Worksheet, ds As Worksheet, x As Long
Dim ltaLR As Long
Dim cmntText As String

With ThisWorkbook
    Set fs = .Worksheets("Filters")
    Set ds = .Worksheets("Data")
End With

LastRow = ds.Cells.Find("*", LookIn:=xlFormulas, Lookat:=xlWhole, _
    SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

ClearSelections
SortData
RemoveComments
DeleteConditionalFormat
UnmergeAllCells

For x = 4 To LastRow

   If fs.Range("F2") = "Yes" Then GoTo Line1 Else GoTo Line2

Line1:        If ds.Cells(x, 1) = ds.Range("E1") And ds.Cells(x, 40) >= _
        fs.Range("C2") And ds.Cells(x, 41) >= fs.Range("C2") Then Goto Line3

Line2:       If ds.Cells(x, 40) >= fs.Range("C2") And ds.Cells(x, 41) >= fs.Range("C2") Then

Line3:     With ThisWorkbook.Worksheets("Test")

            ltaLR = .Cells.Find("*", LookIn:=xlFormulas, Lookat:=xlWhole, _
                SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1

            .Cells(ltaLR, "B").Value = ds.Cells(x, 3) 'League
            .Cells(ltaLR, "B").Resize(2, 1).Merge
            .Cells(ltaLR, "C").Value = ds.Cells(x, 4) 'Home Team
            .Cells(ltaLR + 1, "C").Value = ds.Cells(x, 5) 'Away Team
            .Cells(ltaLR, "D").Value = ds.Cells(x, 81) 'Home Pos
            .Cells(ltaLR + 1, "D").Value = ds.Cells(x, 91) 'Away Pos
            .Cells(ltaLR, "E").Value = ds.Cells(x, 82) 'Home H/A Pos
            .Cells(ltaLR + 1, "E").Value = ds.Cells(x, 92) 'Away H/A Pos
            .Cells(ltaLR, "F").Value = ds.Cells(x, 83) 'Home H/A FH Pos
            .Cells(ltaLR + 1, "F").Value = ds.Cells(x, 93) 'Away H/A FH Pos
            .Cells(ltaLR, "G").Value = ds.Cells(x, 84) 'Home H/A SH Pos
            .Cells(ltaLR + 1, "G").Value = ds.Cells(x, 94) 'Away H/A SH Pos
            .Cells(ltaLR, "H").Value = ds.Cells(x, 85) 'Home Att
            .Cells(ltaLR + 1, "H").Value = ds.Cells(x, 95) 'Away Att
            .Cells(ltaLR, "I").Value = ds.Cells(x, 86) 'Home Def
            .Cells(ltaLR + 1, "I").Value = ds.Cells(x, 96) 'Away Def
            .Cells(ltaLR, "J").Value = ds.Cells(x, 88) 'Home H/A Form
            .Cells(ltaLR + 1, "J").Value = ds.Cells(x, 98) 'Away H/A Form

            'Home Scored
            .Cells(ltaLR, "K").Value = Round((ds.Cells(x, 57).Value _
                / ds.Cells(x, 40).Value) * 100, 0) & "%"
                        If HasComment(.Cells(ltaLR, "K")) Then
                        .Cells(ltaLR, "K").Comment.Delete
                        End If
                        .Cells(ltaLR, "K").AddComment Text:=ds.Cells(x, 57).Value & "/" & ds.Cells(x, 40).Value

             'Away Scored
            .Cells(ltaLR + 1, "K").Value = Round((ds.Cells(x, 71).Value _
                / ds.Cells(x, 41).Value) * 100, 0) & "% (" _
                & ds.Cells(x, 71).Value & "/" & ds.Cells(x, 41).Value & ")"
                        If HasComment(.Cells(ltaLR + 1, "K")) Then
                        .Cells(ltaLR + 1, "K").Comment.Delete
                        End If
                        .Cells(ltaLR + 1, "K").AddComment Text:=ds.Cells(x, 71).Value & "/" & ds.Cells(x, 41).Value

             'Home Conceded
            .Cells(ltaLR, "L").Value = Round((ds.Cells(x, 58).Value _
                / ds.Cells(x, 40).Value) * 100, 0) & "%"
                        If HasComment(.Cells(ltaLR, "L")) Then
                        .Cells(ltaLR, "L").Comment.Delete
                        End If
                        .Cells(ltaLR, "L").AddComment Text:=ds.Cells(x, 58).Value & "/" & ds.Cells(x, 40).Value & ")"

            'Away Conceded
            .Cells(ltaLR + 1, "L").Value = Round((ds.Cells(x, 72).Value _
                / ds.Cells(x, 41).Value) * 100, 0) & "%"
                        If HasComment(.Cells(ltaLR + 1, "L")) Then
                        .Cells(ltaLR + 1, "L").Comment.Delete
                        End If
                        .Cells(ltaLR + 1, "L").AddComment Text:=ds.Cells(x, 72).Value & "/" & ds.Cells(x, 41).Value & ")"

            'Combined HT 0-0
            .Cells(ltaLR, "M").Value = Round(((ds.Cells(x, 229).Value _
                + ds.Cells(x, 243).Value) / (ds.Cells(x, 40).Value _
                + ds.Cells(x, 41).Value)) * 100, 0) & "%"
                        If HasComment(.Cells(ltaLR, "M")) Then
                        .Cells(ltaLR, "M").Comment.Delete
                        End If
                        .Cells(ltaLR, "M").AddComment Text:=(ds.Cells(x, 229).Value + ds.Cells(x, 243).Value) & "/" _
                        & (ds.Cells(x, 40).Value + ds.Cells(x, 41).Value) & ")"
                        .Cells(ltaLR, "M").Resize(2, 1).Merge

            'Combined FT 0-0
            .Cells(ltaLR, "M").Value = Round(((ds.Cells(x, 257).Value _
                + ds.Cells(x, 275).Value) / (ds.Cells(x, 40).Value _
                + ds.Cells(x, 41).Value)) * 100, 0) & "%"
                        If HasComment(.Cells(ltaLR, "M")) Then
                        .Cells(ltaLR, "M").Comment.Delete
                        End If
                        .Cells(ltaLR, "M").AddComment Text:=(ds.Cells(x, 257).Value + ds.Cells(x, 275).Value) & "/" _
                        & (ds.Cells(x, 40).Value + ds.Cells(x, 41).Value) & ")"
                        .Cells(ltaLR, "M").Resize(2, 1).Merge

             'Combined O1.5
            .Cells(ltaLR, "M").Value = Round(((ds.Cells(x, 54).Value + _
                ds.Cells(x, 68).Value) / (ds.Cells(x, 40).Value _
                + ds.Cells(x, 41).Value)) * 100, 0) & "%"
                        If HasComment(.Cells(ltaLR, "M")) Then
                        .Cells(ltaLR, "M").Comment.Delete
                        End If
                        .Cells(ltaLR, "M").AddComment Text:=(ds.Cells(x, 54).Value + ds.Cells(x, 68).Value) & "/" _
                        & (ds.Cells(x, 40).Value + ds.Cells(x, 41).Value) & ")"
                        .Cells(ltaLR, "M").Resize(2, 1).Merge

             'Combined O2.5
            .Cells(ltaLR, "M").Value = Round(((ds.Cells(x, 55).Value _
                + ds.Cells(x, 69).Value) / (ds.Cells(x, 40).Value _
                + ds.Cells(x, 41).Value)) * 100, 0) & "%"
                        If HasComment(.Cells(ltaLR, "M")) Then
                        .Cells(ltaLR, "M").Comment.Delete
                        End If
                        .Cells(ltaLR, "M").AddComment Text:=(ds.Cells(x, 55).Value + ds.Cells(x, 69).Value) & "/" _
                        & (ds.Cells(x, 40).Value + ds.Cells(x, 41).Value) & ")"
                        .Cells(ltaLR, "M").Resize(2, 1).Merge

             'Combined O3.5
            .Cells(ltaLR, "M").Value = Round(((ds.Cells(x, 56).Value _
                + ds.Cells(x, 70).Value) / (ds.Cells(x, 40).Value _
                + ds.Cells(x, 41).Value)) * 100, 0) & "%"
                        If HasComment(.Cells(ltaLR, "M")) Then
                        .Cells(ltaLR, "M").Comment.Delete
                        End If
                        .Cells(ltaLR, "M").AddComment Text:=(ds.Cells(x, 56).Value + ds.Cells(x, 70).Value) & "/" _
                        & (ds.Cells(x, 40).Value + ds.Cells(x, 41).Value) & ")"
                        .Cells(ltaLR, "M").Resize(2, 1).Merge

            'Combined BTS
            .Cells(ltaLR, "M").Value = Round(((ds.Cells(x, 59).Value _
                + ds.Cells(x, 73).Value) / (ds.Cells(x, 40).Value _
                + ds.Cells(x, 41).Value)) * 100, 0) & "%"
                        If HasComment(.Cells(ltaLR, "M")) Then
                        .Cells(ltaLR, "M").Comment.Delete
                        End If
                        .Cells(ltaLR, "M").AddComment Text:=(ds.Cells(x, 59).Value + ds.Cells(x, 73).Value) & "/" _
                        & (ds.Cells(x, 40).Value + ds.Cells(x, 41).Value) & ")"
                        .Cells(ltaLR, "M").Resize(2, 1).Merge

            'Combined GP 1-0
            .Cells(ltaLR, "M").Value = Round(((ds.Cells(x, 144).Value _
                + ds.Cells(x, 159).Value) / (ds.Cells(x, 40).Value _
                + ds.Cells(x, 41).Value)) * 100, 0) & "%"
                        If HasComment(.Cells(ltaLR, "M")) Then
                        .Cells(ltaLR, "M").Comment.Delete
                        End If
                        .Cells(ltaLR, "M").AddComment Text:=(ds.Cells(x, 144).Value + ds.Cells(x, 159).Value) & "/" _
                        & (ds.Cells(x, 40).Value + ds.Cells(x, 41).Value) & ")"
                        .Cells(ltaLR, "M").Resize(2, 1).Merge

             'Combined GP 0-1
            .Cells(ltaLR, "M").Value = Round(((ds.Cells(x, 147).Value _
                + ds.Cells(x, 162).Value) / (ds.Cells(x, 40).Value _
                + ds.Cells(x, 41).Value)) * 100, 0) & "%"
                        If HasComment(.Cells(ltaLR, "M")) Then
                        .Cells(ltaLR, "M").Comment.Delete
                        End If
                        .Cells(ltaLR, "M").AddComment Text:=(ds.Cells(x, 147).Value + ds.Cells(x, 162).Value) & "/" _
                        & (ds.Cells(x, 40).Value + ds.Cells(x, 41).Value) & ")"
                        .Cells(ltaLR, "M").Resize(2, 1).Merge
        End With
     End If

Next x

Application.ScreenUpdating = True

End Sub

我要选择的两个IF语句之间的唯一区别是以下代码行:

If ds.Cells(x, 1) = ds.Range("E1")

我尝试使用IF GOTO THEN ELSE,但这不起作用。谁能指出我要去哪里了?

3 个答案:

答案 0 :(得分:1)

似乎您要根据两个条件之一来处理相同的代码。这些条件可以堆叠到具有多个And / Or运算符的单个If语句中,但为清楚起见,我将它们分配给两个布尔变量。

dim b1 as boolean, b2 as boolean

...

For x = 4 To LastRow

   b1 = cbool(ds.Cells(x, 1) = ds.Range("E1") And _
              ds.Cells(x, 40) >= fs.Range("C2") And _
              ds.Cells(x, 41) >= fs.Range("C2"))
   b2 = cbool(ds.Cells(x, 40) >= fs.Range("C2") And _
              ds.Cells(x, 41) >= fs.Range("C2"))

   If (fs.Range("F2") = "Yes" and b1) Or (fs.Range("F2") <> "Yes" and b2) Then

      With ThisWorkbook.Worksheets("Test")

   ' rest of code ...

答案 1 :(得分:0)

为什么要制作这么复杂的东西?您不能简单地这样做:

If fs.Range("F2") = "Yes" Then
    If ds.Cells(x, 1) = ds.Range("E1") And ds.Cells(x, 40) >= _
    fs.Range("C2") And ds.Cells(x, 41) >= fs.Range("C2") Then 
        "Here line 3 code"

Else 

End If

答案 2 :(得分:0)

Erm ...嵌套“ IF”语句吗?

If [A1]="Yes" Then
    If First_condition = SomeValue Then
        'Do Stuff
    End If
Else
    If Second_condition = DifferentValue Then
        'Do other Stuff
    End If
End If

如果需要,您可以做的事情是设置一个布尔变量(True / False)供以后使用:

Dim BoolTest AS Boolean

If [A1]="Yes" Then
    BoolTest = (First_condition = SomeValue)
Else
    BoolTest = (Second_condition = DifferentValue)
End If

If BoolTest Then
    'Do Stuff
End If