簡體   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