简体   繁体   English

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

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

I need to check if the active worksheet is the first worksheet in the workbook. 我需要检查活动工作表是否是工作簿中的第一个工作表。 This code isn't working at all, but I'm hoping I was on the right track. 这段代码根本不起作用,但是我希望自己走在正确的轨道上。

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

This is simple 这很简单

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

You can't compare reference types with = and <> - you have to use Is : 您不能将引用类型与=<>进行比较-您必须使用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

Here you go: 干得好:

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