Worksheet
对象是
Worksheets
集合的成员。
Worksheets
集合包含工作簿中的所有
Worksheet
对象。
Worksheet
对象也是
Sheets
集合的成员。
Sheets
集合包含工作簿中所有的工作表(图表工作表和工作表)。
使用
Worksheets
(
索引
) (其中
index
是工作表索引号或名称)可返回单个
Worksheet
对象。 下例隐藏活动工作簿中的工作表一。
Worksheets(1).Visible = False
工作表索引号指示工作表在工作簿的选项卡栏的位置。 Worksheets(1)
是工作簿中的第一个(最左边)工作表,并且 Worksheets(Worksheets.Count)
是最后一个。 所有工作表都包含在索引计数中,即使它们处于隐藏状态。
工作表名称显示在工作表的选项卡上。 使用 Name 属性可设置或返回工作表名称。 以下示例保护 Sheet1 上的方案。
Dim strPassword As String
strPassword = InputBox ("Enter the password for the worksheet")
Worksheets("Sheet1").Protect password:=strPassword, scenarios:=True
当工作表是活动工作表时,可以使用 ActiveSheet 属性来引用它。 下例使用 Activate 方法激活 Sheet1,将页面方向设置为横向,然后打印该工作表。
Worksheets("Sheet1").Activate
ActiveSheet.PageSetup.Orientation = xlLandscape
ActiveSheet.PrintOut
此示例使用 BeforeDoubleClick 事件在记事本中打开一组指定的文件。 若要使用此示例,工作表必须包含以下数据:
单元格 A1 中必须包含要打开的文件的名称,每个文件使用逗号和空格进行分隔。
单元格 D1 必须包含指向记事本文件的位置路径。
单元格 D2 必须包含指向记事本程序的位置路径。
单元格 D3 必须包含记事本文件 (txt) 的文件扩展名,中间不能有句点。
当您双击单元格 A1 时,在单元格 A1 中指定的文件将在记事本中打开。
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
Activate
BeforeDelete
BeforeDoubleClick
BeforeRightClick
Calculate
Change
Deactivate
FollowHyperlink
LensGalleryRenderComplete
PivotTableAfterValueChange
PivotTableBeforeAllocateChanges
PivotTableBeforeCommitChanges
PivotTableBeforeDiscardChanges
PivotTableChangeSync
PivotTableUpdate
SelectionChange
TableUpdate
Activate
Calculate
ChartObjects
CheckSpelling
CircleInvalid
ClearArrows
ClearCircles
Evaluate
ExportAsFixedFormat
OLEObjects
Paste
PasteSpecial
PivotTables
PivotTableWizard
PrintOut
PrintPreview
Protect
ResetAllPageBreaks
SaveAs
Scenarios
Select
SetBackgroundPicture
ShowAllData
ShowDataForm
Unprotect
XmlDataQuery
XmlMapQuery
AutoFilter
AutoFilterMode
Cells
CircularReference
CodeName
CommentsThreaded
ConsolidationFunction
ConsolidationOptions
ConsolidationSources
Creator
CustomProperties
DisplayPageBreaks
DisplayRightToLeft
EnableAutoFilter
EnableCalculation
EnableFormatConditionsCalculation
EnableOutlining
EnablePivotTable
EnableSelection
FilterMode
HPageBreaks
Hyperlinks
ListObjects
MailEnvelope
Names
Outline
PageSetup
Parent
Previous
PrintedCommentPages
ProtectContents
ProtectDrawingObjects
ProtectionMode
ProtectScenarios
QueryTables
Range
ScrollArea
Shapes
StandardHeight
StandardWidth
TransitionExpEval
TransitionFormEntry
UsedRange
Visible
VPageBreaks
Excel 对象模型引用
支持和反馈
有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。