Skip to content

Latest commit

 

History

History
77 lines (50 loc) · 2.15 KB

Excel.Application.ActiveSheet.md

File metadata and controls

77 lines (50 loc) · 2.15 KB
title keywords f1_keywords api_name ms.assetid ms.date ms.localizationpriority
Application.ActiveSheet property (Excel)
vbaxl10.chm132078
vbaxl10.chm132078
Excel.Application.ActiveSheet
6ed42d87-2ad5-eecc-ad5b-4c92617a04bc
04/04/2019
high

Application.ActiveSheet property (Excel)

Returns an object that represents the active sheet (the sheet on top) in the active workbook or in the specified window or workbook. Returns Nothing if no sheet is active.

Syntax

expression.ActiveSheet

expression A variable that represents an Application object.

Remarks

If you don't specify an object qualifier, this property returns the active sheet in the active workbook.

If a workbook appears in more than one window, the ActiveSheet property may be different in different windows.

Example

This example displays the name of the active sheet.

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

This example creates a print preview of the active sheet that has the page number at the top of column B on each page.

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

[!includeSupport and feedback]