種ヶ島

いろいろなエッセイを書いてます。

マクロ 選択範囲の行列の取得


スポンサーリンク

選択した範囲の右上と左下の行列番号を取得するマクロです。

 

 

Sub 選択範囲の行列()
'選択範囲の左上と右下の行列数を取得する
'範囲の左上のセルは、選択範囲の1番目なのでSelection(1)になる
'右に向かって番号が増える
'最後の右下は、範囲の最後のセルとなり、選択したセルの数なので、『Selection.Count』番目となる
'よってSelection(Selection.Count)になる

 

Dim r, c, mr, mc As Variant

Selection.CurrentRegion.Select ' Ctrl+Shift+*全選択

r = Selection(1).Row
c = Selection(1).Column
mr = Selection(Selection.Count).Row
mc = Selection(Selection.Count).Column


Range("a20") = r
Range("a21") = c
Range("a22") = mr
Range("a23") = mc

End Sub