この記事では、Worksheetオブジェクトとその関連機能について説明しています。Worksheetオブジェクトはワークブック内のすべてのワークシートを含むWorksheetsコレクションの一部であり、Sheetsコレクションにも属します。特定のWorksheetオブジェクトを取得するには、Worksheets(インデックス)を使用します。例として、ワークシートの表示設定や保護、アクティブなシートの使用、ダブルクリックイベントを使ってNotepadでファイルを開く方法が紹介されています。ワークシートに関連するプロパティやメソッドについても説明されています。
ワークシートオブジェクトの紹介
はじめに
この記事では、Excel VBAにおけるワークシートオブジェクトについて説明します。ワークシートオブジェクトは、Excelのワークブックに含まれる各シートを指し、データの入力や操作を行う際に利用されます。
ワークシートオブジェクトとは
ワークシートオブジェクトは、Worksheets コレクションのメンバーであり、コレクションにはワークブック内の全てのワークシートが含まれています。また、ワークシートオブジェクトはSheets コレクションのメンバーでもあり、こちらにはワークブック内の全てのシート(チャートシートとワークシートの両方)が含まれます。
例
ワークシートオブジェクトを操作するために、次のようなVBAコードを使用します。
Worksheets(1).Visible = False
上記のコードは、アクティブなワークブックの最初のワークシートを非表示にします。ワークシートのインデックス番号は、ワークブックのタブバーにおけるワークシートの位置を示します。たとえば、Worksheets(1)
は最初のワークシートを指し、Worksheets(Worksheets.Count)
は最後のワークシートを指します。
ワークシートの名前はタブに表示されます。次のコードは、Sheet1のシナリオを保護する例です。
Dim strPassword As String
strPassword = InputBox("ワークシートのパスワードを入力してください")
Worksheets("Sheet1").Protect password:=strPassword, scenarios:=True
アクティブシートの使用
アクティブなシートを扱う場合、次のコードを使用してSheet1をアクティブにし、ページの向きを横向きに設定して印刷することができます。
Worksheets("Sheet1").Activate
ActiveSheet.PageSetup.Orientation = xlLandscape
ActiveSheet.PrintOut
ダブルクリックイベントを使用したファイルのオープン
この例では、ワークシートの特定セルをダブルクリックすることで、指定されたファイルをNotepadで開くコードを示します。ワークシートには、次のようなデータが必要です:
- セルA1には開くファイル名をカンマで区切って記載します。
- セルD1にはNotepadファイルのパスを、D2にはNotepadの実行ファイルのパスを、D3には拡張子(txtなど)を記載します。
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
' 変数の定義
Dim sFile As String, sPath As String, sTxt As String, sExe As String, sSfx As String
' A1をダブルクリックしていない場合、関数を終了
If Target.Address <> "$A$1" Then Exit Sub
' ダブルクリックした場合、デフォルトの動作を無効にする
Cancel = True
' セルの情報を元にファイルパス等を設定
sPath = Range("D1").Value
sExe = Range("D2").Value
sSfx = Range("D3").Value
sFile = Range("A1").Value
' ファイル名のスペースを削除
sFile = WorksheetFunction.Substitute(sFile, " ", "")
' リスト内の各ファイルを処理
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
' 最後のファイル名を処理
sTxt = sPath & "\" & sFile & "." & sSfx
If Dir(sTxt) <> "" Then Shell sExe & " " & sTxt, vbNormalNoFocus
End Sub
まとめ
この記事では、Excel VBAにおけるワークシートオブジェクトの概要と、主な使用例について紹介しました。ワークシートオブジェクトを利用することで、Excel内でのデータの管理や操作がスムーズに行えます。さらに詳しい情報については、Office VBAサポートとフィードバックを参照してください。
————-
Worksheet object (Excel) | Microsoft Learn
Source link
The article provides an overview of the Worksheet object in Microsoft Excel VBA, detailing its relation to the Worksheets and Sheets collections in a workbook. It explains how to access a specific Worksheet using its index or name and provides examples of various operations that can be performed on worksheets, such as hiding a worksheet, protecting it with a password, and altering its print settings.
Key points include:
-
Worksheet and Collections: The Worksheet object is part of the Worksheets collection (all worksheets in a workbook) and the Sheets collection (which includes both worksheets and chart sheets).
-
Accessing Worksheets: You can use
Worksheets(index)
to access a Worksheet, where the index is either the position number (starting from 1) or the name of the worksheet. -
ActiveSheet Property: When a worksheet is active, you can refer to it using the ActiveSheet property, allowing for operations such as changing page orientation and printing.
-
Example Script: An example code snippet is provided that utilizes the BeforeDoubleClick event to open specified text files in Notepad when a cell is double-clicked. The data necessary for this functionality is expected in specific cells of the worksheet.
- Additional Resources: The article suggests seeking support and feedback for further guidance on Office VBA.
Overall, the article serves as a tutorial for manipulating worksheets in Excel using VBA.
コメント