Worksheet オブジェクト (Excel)

ワークシートを表します。

注釈

Worksheet オブジェクトは Worksheets コレクションのメンバーです。 Worksheets コレクションには、ブックにあるすべての Worksheet オブジェクトが含まれています。

Worksheet オブジェクトは、Sheets コレクションのメンバーでもあります。 Sheets コレクションには、ブックにあるすべてのシート (グラフ シート、ワークシートの両方) が含まれています。

1 つの Worksheet オブジェクトを返すには、Worksheets (index) を使用します。index はワークシートのインデックス番号または名前です。 次の例では、作業中のブックのワークシート 1 を非表示にします。

Worksheets(1).Visible = False

ワークシートのインデックス番号は、ブックのシート見出しの並び順に対応します。 Worksheets(1) はブックの最初、つまりシート見出しが最も左側にあるワークシートであり、 Worksheets(Worksheets.Count) は最後のワークシートです。 非表示になっている場合でも、すべてのワークシートはインデックス数に含まれます。

ワークシート名はシート見出しに表示されます。 Name プロパティを使用して、ワークシート名を設定または返します。 次の使用例は、シート 1 のシナリオを保護します。

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

ワークシートがアクティブなシートの場合は、 ActiveSheet プロパティを使用して参照できます。 次の使用例は、 Activate メソッドを使用してシート 1 を選択し、印刷の向きを横に設定して、ワークシートを印刷します。

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, ","))
   Loop
   
   'Finish off the last file name in the list
   sTxt = sPath & "\" & sFile & "." & sSfx
   If Dir(sTxt) <> "" Then Shell sExe & " " & sTxt, vbNormalNoFocus
End Sub

イベント

メソッド

プロパティ

関連項目

サポートとフィードバック

Office VBA またはこの説明書に関するご質問やフィードバックがありますか? サポートの受け方およびフィードバックをお寄せいただく方法のガイダンスについては、Office VBA のサポートおよびフィードバックを参照してください。