エクセルVBA 【終点自動】A~E列を選択するマクロ
A~E列を選択するマクロを7つ紹介します。行数はA列にあわせて自動的に決まります。始点はA1セルです。
Range(始点、終点)パターン
Range(Cells(1, "A"), Cells(Cells(Rows.Count, "A").End(xlUp).Row, "E")).Select
Range(Cells(1, 1), Cells(Cells(Rows.Count, 1).End(xlUp).Row, 5)).Select
Range(Range("A1"), Range("E" & Cells(Rows.Count, "A").End(xlUp).Row)).Select
Range(Range("A1"), Range("E" & Range("A" & Rows.Count).End(xlUp).Row)).Select
Range("始点:終点")パターン
Range("A1:E" & Cells(Rows.Count, "A").End(xlUp).Row).Select
Range("A1:E" & Cells(Rows.Count, 1).End(xlUp).Row).Select
Range("A1:E" & Range("A" & Rows.Count).End(xlUp).Row).Select
結果はこうなります。A~E列が、A列の入力されている範囲で選択されます。
行数をC列に合わせて選択する場合は次のようになります。始点はA1セルです。
Range(始点、終点)パターン
Range(Cells(1, "A"), Cells(Cells(Rows.Count, "C").End(xlUp).Row, "E")).Select
Range(Cells(1, 1), Cells(Cells(Rows.Count, 3).End(xlUp).Row, 5)).Select
Range(Range("A1"), Range("E" & Cells(Rows.Count, "C").End(xlUp).Row)).Select
Range(Range("A1"), Range("E" & Range("C" & Rows.Count).End(xlUp).Row)).Select
Range("始点:終点")パターン
Range("A1:E" & Cells(Rows.Count, "C").End(xlUp).Row).Select
Range("A1:E" & Cells(Rows.Count, 3).End(xlUp).Row).Select
Range("A1:E" & Range("C" & Rows.Count).End(xlUp).Row).Select
結果はこうなります。C列の行数に合わせてA~E列が選択されます。
行数をE列に合わせたい場合は次のようになります。始点はA1セルです。次の9つの例をあげましたが、どれも同じ結果になります。
Range(始点、終点)パターン
Range(Cells(1, "A"), Cells(Cells(Rows.Count, "E").End(xlUp).Row, "E")).Select
Range(Cells(1, 1), Cells(Cells(Rows.Count, 5).End(xlUp).Row, 5)).Select
Range(Range("A1"), Cells(Rows.Count, "E").End(xlUp)).Select
Range(Range("A1"), Cells(Rows.Count, 5).End(xlUp)).Select
Range(Cells(1, "A"), Cells(Rows.Count, "E").End(xlUp)).Select
Range(Cells(1, 1), Cells(Rows.Count, 5).End(xlUp)).Select
Range("始点:終点")パターン
Range("A1:E" & Cells(Rows.Count, "E").End(xlUp).Row).Select
Range("A1:E" & Cells(Rows.Count, 5).End(xlUp).Row).Select
Range(Range("A1"), Range("E" & Cells(Rows.Count, "E").End(xlUp).Row)).Select
Cells(Rows.Count, "E")の部分は、Range("E" & Rows.Count)と書き変えても同じです。
結果はこうなります。E列の行数にあわせてA~E列が選択されています。
行数を表の一番多い行にあわせる場合はこう記述します。始点はA1セルです。
Range("A1:E" & Cells.CurrentRegion.Rows.Count).Select
結果はこうなります。F8に入力がありますので8行目まで選択されています。
A~E列に限定せず、A1セルを含む表全体を選択したい場合は次のように記述します。始点はA1セルです。次の7
つの例はどれも同じ結果になります(厳密には表の中に完全な空白列や行があれば少し結果が変わってきます)。
Range(Cells(1, 1), Cells.SpecialCells(xlLastCell)).Select
Range(Range("A1"), Cells.SpecialCells(xlLastCell)).Select
Range("A1").CurrentRegion.Select
Cells.CurrentRegion.Select
Cells(1).CurrentRegion.Select
Range(Cells(1, "A"), Cells(Cells.CurrentRegion.Rows.Count, Cells.CurrentRegion.Columns.Count)).Select
Range(Range("A1") , Cells(Cells.CurrentRegion.Rows.Count, Cells.CurrentRegion.Columns.Count)).Select
結果はこんな感じです。
おすすめ記事