Worksheet 对象 (Excel)Worksheet object (Excel)

代表一个工作表。Represents a worksheet.


工作表对象是**工作表** 集合的成员。The Worksheet object is a member of the Worksheets collection. Worksheets 集合包含工作簿中的所有 Worksheet 对象。The Worksheets collection contains all the Worksheet objects in a workbook.

工作表对象也是**工作表** 集合的一个成员。The Worksheet object is also a member of the Sheets collection. Sheets 集合包含工作簿中所有的工作表(图表工作表和工作表)。The Sheets collection contains all the sheets in the workbook (both chart sheets and worksheets).


使用**工作** 表 (索引), 其中_索引_是工作表索引号或名称, 返回单个的工作表对象。Use Worksheets (index), where index is the worksheet index number or name, to return a single Worksheet object. 下例隐藏活动工作簿中的工作表一。The following example hides worksheet one in the active workbook.

Worksheets(1).Visible = False

工作表索引号指示工作表在工作簿的选项卡栏的位置。The worksheet index number denotes the position of the worksheet on the workbook's tab bar. Worksheets(1) 是工作簿中的第一个(最左边)工作表,并且 Worksheets(Worksheets.Count) 是最后一个。Worksheets(1) is the first (leftmost) worksheet in the workbook, and Worksheets(Worksheets.Count) is the last one. 所有工作表都包含在索引计数中, 即使它们是隐藏的也是如此。All worksheets are included in the index count, even if they are hidden.

工作表名称显示在工作表的选项卡上。The worksheet name is shown on the tab for the worksheet. 使用**Name** 属性设置或返回工作表名称。Use the Name property to set or return the worksheet name. 以下示例保护 Sheet1 上的方案。The following example protects the scenarios on Sheet1.

Dim strPassword As String 
strPassword = InputBox ("Enter the password for the worksheet") 
Worksheets("Sheet1").Protect password:=strPassword, scenarios:=True

当工作表是活动工作表时, 您可以使用**ActiveSheet** 属性来引用它。When a worksheet is the active sheet, you can use the ActiveSheet property to refer to it. 下例使用 Activate 方法激活 Sheet1,将页面方向设置为横向,然后打印该工作表。The following example uses the Activate method to activate Sheet1, sets the page orientation to landscape mode, and then prints the worksheet.

ActiveSheet.PageSetup.Orientation = xlLandscape 

此示例使用**BeforeDoubleClick** 事件在记事本中打开一组指定的文件。This example uses the BeforeDoubleClick event to open a specified set of files in Notepad. 若要使用此示例, 您的工作表必须包含以下数据:To use this example, your worksheet must contain the following data:

  • 单元格 A1 中必须包含要打开的文件的名称,每个文件使用逗号和空格进行分隔。Cell A1 must contain the names of the files to open, each separated by a comma and a space.
  • 单元格 D1 必须包含指向记事本文件的位置路径。Cell D1 must contain the path to where the Notepad files are located.
  • 单元格 D2 必须包含指向记事本程序的位置路径。Cell D2 must contain the path to where the Notepad program is located.
  • 单元格 D3 必须包含记事本文件 (txt) 的文件扩展名,中间不能有句点。Cell D3 must contain the file extension, without the period, for the Notepad files (txt).

当您双击单元格 A1 时,在单元格 A1 中指定的文件将在记事本中打开。When you double-click cell A1, the files specified in cell A1 are opened in Notepad.

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
   'Define your variables.
   Dim sFile As String, sPath As String, sTxt As String, sExe As String, sSfx As String
   'If you did not double-click on A1, then exit the function.
   If Target.Address <> "$A$1" Then Exit Sub
   'If you did double-click on A1, then override the default double-click behavior with this function.
   Cancel = True
   'Set the path to the files, the path to Notepad, the file extension of the files, and the names of the files,
   'based on the information on the worksheet.
   sPath = Range("D1").Value
   sExe = Range("D2").Value
   sSfx = Range("D3").Value
   sFile = Range("A1").Value
   'Remove the spaces between the file names.
   sFile = WorksheetFunction.Substitute(sFile, " ", "")
   'Go through each file in the list (separated by commas) and
   'create the path, call the executable, and move on to the next comma.
   Do While InStr(sFile, ",")
      sTxt = sPath & "\" & Left(sFile, InStr(sFile, ",") - 1) & "." & sSfx
      If Dir(sTxt) <> "" Then Shell sExe & " " & sTxt, vbNormalFocus
      sFile = Right(sFile, Len(sFile) - InStr(sFile, ","))
   'Finish off the last file name in the list
   sTxt = sPath & "\" & sFile & "." & sSfx
   If Dir(sTxt) <> "" Then Shell sExe & " " & sTxt, vbNormalNoFocus
End Sub




另请参阅See also

支持和反馈Support and feedback

有关于 Office VBA 或本文档的疑问或反馈?Have questions or feedback about Office VBA or this documentation? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.