编译错误:阻止如果没有结束If

时间:2016-02-16 16:36:21

标签: excel vba excel-vba

我试图每隔6秒在3张纸之间切换,当我添加标题为Dash的页面时,它处理了2张纸我得到了编译错误阻止如果没有结束

Sub Swap_Sheets()
    Dim dTime As Date

    dTime = Now + TimeValue("00:00:6")
    Application.OnTime dTime, "Swap_Sheets"

    If ActiveSheet.Name = "Plant View" Then
        Sheets("Dash").Activate
    Else
        If ActiveSheet.Name = "Dash" Then
            Sheets("Target").Activate
        Else
            Sheets("Plant View").Activate
        End If
        If gciConsole.CheckBox1.Value = False Then
            Application.OnTime dTime, "Swap_Sheets", , False
        End If
    End Sub

2 个答案:

答案 0 :(得分:3)

您需要摆脱ElseIf之间的空间,以便:

Sub Swap_Sheets()

Dim dTime As Date

dTime = Now + TimeValue("00:00:6")
Application.OnTime dTime, "Swap_Sheets"

If ActiveSheet.Name = "Plant View" Then
    Sheets("Dash").Activate
ElseIf ActiveSheet.Name = "Dash" Then '<~~~ Notice change here'
    Sheets("Target").Activate
Else
    Sheets("Plant View").Activate
End If

If gciConsole.CheckBox1.Value = False Then
    Application.OnTime dTime, "Swap_Sheets", , False
End If

End Sub

答案 1 :(得分:2)

您需要将第二个语句更改为ElseIf

If ActiveSheet.Name = "Plant View" Then
    Sheets("Dash").Activate
ElseIf ActiveSheet.Name = "Dash" Then
    Sheets("Target").Activate
Else
    Sheets("Plant View").Activate
End If

或在最后加入End If

If ActiveSheet.Name = "Plant View" Then
    Sheets("Dash").Activate
Else
    If ActiveSheet.Name = "Dash" Then
        Sheets("Target").Activate
    Else
        Sheets("Plant View").Activate
    End If
End If