繁体   English   中英

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

[英]Excel VBA - check if active worksheet is first worksheet

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

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

这很简单

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

您不能将引用类型与=<>进行比较-您必须使用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

干得好:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM