Visual Basic For Applications Tips


範囲の取得


範囲を取得するには
Set SourceRange = Cells.SpecialCells(xlVisible) '全ての可視セル
Set SourceRange = Cells.SpecialCells(xlFormulas) '数式のセル
Set SourceRange = Cells.SpecialCells(xlConstants) '定数のセル
Set SourceRange = Cells.SpecialCells(xlConstants, xlTextValues) '定数で文字を含むセル

選択されている範囲を取得するにはオブジェクト式をCellsからSelectionに変更します。例えば

Set Sourcerange = Selection.SpecialCells(xlVisible) '全ての可視セル

とします。

下のサンプルは選択されている範囲の行、列、値をイミディエイトウィンドウに出力します。

Dim SourceRange As Range
Dim item As Variant

'選択されている範囲を取得します。
Set sourcerange = Selection.SpecialCells(xlVisible) '選択されている全ての可視セル

'選択されているセルのループ
For Each item In SourceRange
  Debug.Print item.Row & "行" & item.Column & "列" & item.Value
Next