Excel VBA - 检查活动工作表是否是第一个工作表

时间:2017-01-10 17:30:16

标签: excel vba excel-vba

我需要检查活动工作表是否是工作簿中的第一个工作表。这段代码根本不起作用,但我希望我走在正确的轨道上。

Sub CheckFirstSheet()
If Sheets(1) = ActiveSheet Then MsgBox "This is the first worksheet."
If Sheets(1) <> ActiveSheet Then MsgBox "This is not the first worksheet."
End Sub

3 个答案:

答案 0 :(得分:3)

这很简单

Sub test()
If ActiveSheet.Index = 1 Then MsgBox "This is the first worksheet."
If ActiveSheet.Index <> 1 Then MsgBox "This is not the first worksheet."
End Sub

答案 1 :(得分:1)

您无法将参考类型与=<>进行比较 - 您必须使用Is

Sub CheckFirstSheet()
    If Sheets(1) Is ActiveSheet Then
        MsgBox "This is the first worksheet."
    Else
        MsgBox "This is not the first worksheet."
    End If
End Sub

答案 2 :(得分:1)

你走了:

Sub CheckFirstSheet()

If ActiveSheet.Index = 1 Then
    MsgBox "This is the first worksheet."
Else
    MsgBox "This is not the first worksheet."
End If

End Sub