Application.ActiveSheet 屬性 (Excel)

可傳回物件,此物件代表使用中活頁簿中或者指定視窗或活頁簿中的使用中工作表 (最上層工作表)。 如果沒有現用工作表,則會傳回 Nothing

語法

expression.ActiveSheet

expression 代表 Application 物件的變數。

備註

如果您未指定物件辨識符號,則此屬性會傳回使用中活頁簿內的現用工作表。

如果活頁簿出現在一個以上的視窗中,則不同視窗中的 ActiveSheet 屬性可能會不同。

範例

這個範例會顯示使用中工作表的名稱。

MsgBox "The name of the active sheet is " & ActiveSheet.Name

此範例會建立現用工作表的預覽列印,在每頁的欄位 B 頂端有頁碼。

Sub PrintSheets()

   'Set up your variables.
   Dim iRow As Integer, iRowL As Integer, iPage As Integer
   'Find the last row that contains data.
   iRowL = Cells(Rows.Count, 1).End(xlUp).Row
   
   'Define the print area as the range containing all the data in the first two columns of the current worksheet.
   ActiveSheet.PageSetup.PrintArea = Range("A1:B" & iRowL).Address
   
   'Select all the rows containing data.
   Rows(iRowL).Select
   
   'display the automatic page breaks
   ActiveSheet.DisplayAutomaticPageBreaks = True
   Range("B1").Value = "Page 1"
   
   'After each page break, go to the next cell in column B and write out the page number.
   For iPage = 1 To ActiveSheet.HPageBreaks.Count
      ActiveSheet.HPageBreaks(iPage) _
         .Location.Offset(0, 1).Value = "Page " & iPage + 1
   Next iPage
   
   'Show the print preview, and afterwards remove the page numbers from column B.
   ActiveSheet.PrintPreview
   Columns("B").ClearContents
   Range("A1").Select
End Sub

支援和意見反應

有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應