おとしごと

子育てとか、日々の生活とかで感じたことを

【Excel VBA】選択されたセル範囲を取得

EXCELVBAを少し勉強することにした。

マウスでセルを範囲指定し、選択されたセルの範囲を取得できるか。

素人なので、これでも結構悩んだ。

こんな感じで書いてみた。

選択範囲を取得して、セルのA1~B8に結果を入力して確認する。

Sub マウスで選択している範囲()
Dim SelectionArea As Range
Set SelectionArea = Selection

Cells(1, 1) = "選択範囲"
Cells(2, 1) = "選択行数"
Cells(3, 1) = "選択列数"
Cells(4, 1) = "開始行番号"
Cells(5, 1) = "開始列番号"
Cells(6, 1) = "終了行番号"
Cells(7, 1) = "終了列番号"
Cells(8, 1) = "予備"

Cells(1, 2) = Selection.Address
Cells(2, 2) = CStr(Selection.Row)
Cells(3, 2) = CStr(Selection.Column)
Cells(4, 2) = CStr(SelectionArea.Cells(1).Row)
Cells(5, 2) = CStr(SelectionArea.Cells(1).Column)
Cells(6, 2) = CStr(SelectionArea.Cells(SelectionArea.Count).Row)
Cells(7, 2) = CStr(SelectionArea.Cells(SelectionArea.Count).Column)
Cells(8, 2) = "予備"

End Sub

一応、セル選択範囲を取得できたみたい。

まず必要なのは、これだけ???

Dim SelectionArea As Range
Set SelectionArea = Selection

以上。