この記事では、指定された範囲からオフセットされた範囲を表すRangeオブジェクトを返す方法について説明しています。構文は「expression.Offset(RowOffset, ColumnOffset)」で、RowOffsetとColumnOffsetは任意の引数です。RowOffsetは行数を指定し、正の値は下方向、負の値は上方向にオフセットします。ColumnOffsetは列数を指定し、正の値は右方向、負の値は左方向にオフセットします。例として、アクティブなセルから3列右、3行下のセルをアクティブにするコードが示されています。
Excel VBAにおけるRange.Offsetメソッドの使い方
Excel VBAでは、データ操作や自動化の変数として「Range」オブジェクトを利用します。その中でも「Offset」メソッドは、特定の範囲からオフセットを指定して新しい範囲を返す非常に便利な機能です。このメソッドの使い方を以下に詳しく解説します。
Offsetメソッドの概要
「Offset」メソッドは、指定した範囲から行数および列数のオフセットを指定して新しい範囲を返します。このメソッドを使用することで、データの取得や操作が行いやすくなります。
構文
expression.Offset(RowOffset, ColumnOffset)
- expression:範囲を表す変数(Rangeオブジェクト)
- RowOffset:オフセットする行数(省略可能)
- ColumnOffset:オフセットする列数(省略可能)
パラメータ
名称 | 必須/省略 | データ型 | 説明 |
---|---|---|---|
RowOffset | 省略可能 | Variant | 範囲をオフセットする行数。正の値は下方、負の値は上方にオフセットされます。デフォルト値は0です。 |
ColumnOffset | 省略可能 | Variant | 範囲をオフセットする列数。正の値は右方、負の値は左方にオフセットされます。デフォルト値は0です。 |
使用例
1. 簡単なセルのオフセット
次の例では、現在アクティブなセルから3列右、3行下のセルをアクティブにします。
Worksheets("Sheet1").Activate
ActiveCell.Offset(RowOffset:=3, ColumnOffset:=3).Activate
2. テーブルの選択
次の例では、アクティブなセルがテーブル内にあると仮定した上で、ヘッダー行を選択せずにテーブルを選択します。
Set tbl = ActiveCell.CurrentRegion
tbl.Offset(1, 0).Resize(tbl.Rows.Count - 1, tbl.Columns.Count).Select
3. 省略したパラメータの使用
行オフセットや列オフセットが0の場合は、次のように省略することができます。
セルD1を選択
Range("A1").Offset(, 3).Select
セルA5を選択
Range("A1").Offset(4).Select
サポートとフィードバック
Office VBAやこのドキュメントに関する質問やフィードバックがある場合は、Office VBAサポートとフィードバックをご覧ください。
まとめ
「Offset」メソッドは、Excel VBAを使用する際にデータの移動や範囲の操作を行うための強力なツールです。適切に活用することで、より効率的なデータ処理が可能になります。ぜひ、試してみてください。
————-
Range.Offset property (Excel) | Microsoft Learn
Source link
The article discusses the use of the Offset method in VBA (Visual Basic for Applications) for Excel, which enables users to obtain a Range object that is shifted from a specified range by a certain number of rows and columns.
Key Points:
-
Syntax: The syntax for the Offset method is
expression.Offset(RowOffset, ColumnOffset)
, whereexpression
is a Range object. -
Parameters:
- RowOffset: This is an optional parameter that allows you to specify the number of rows to shift (positive values move downward, negative values move upward, and 0 means no shift).
- ColumnOffset: This is also optional and defines the number of columns to shift (positive values move right, negative values move left, and 0 means no shift).
-
Examples:
- To activate a cell three columns to the right and three rows down from the currently active cell on "Sheet1", you can use:
Worksheets("Sheet1").Activate ActiveCell.Offset(rowOffset:=3, columnOffset:=3).Activate
- To select a specific table without including the header row:
Set tbl = ActiveCell.CurrentRegion tbl.Offset(1, 0).Resize(tbl.Rows.Count - 1, tbl.Columns.Count).Select
- To activate a cell three columns to the right and three rows down from the currently active cell on "Sheet1", you can use:
-
Omission of Zeroes: If either RowOffset or ColumnOffset is zero, it can be omitted in the syntax.
- Additional Examples:
- Selecting cell D1 can be done with
Range("A1").Offset(, 3).Select
. - Selecting cell A5 can be achieved using
Range("A1").Offset(4).Select
.
- Selecting cell D1 can be done with
The article also invites users to provide feedback and seek assistance regarding Office VBA.
コメント