google.com, pub-4426889494877513, DIRECT, f08c47fec0942fa0 虹と雪、そして桜: 2020年10月の2件の記事

« 2020年5月 | トップページ | 2021年10月 »

2020/10/13

エクセル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列の入力されている範囲で選択されます。

4

 

行数を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列が選択されます。

5

 

行数を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列が選択されています。

6

 

行数を表の一番多い行にあわせる場合はこう記述します。始点はA1セルです。

Range("A1:E" & Cells.CurrentRegion.Rows.Count).Select

結果はこうなります。F8に入力がありますので8行目まで選択されています。

7

 

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

結果はこんな感じです。

8

おすすめ記事

エクセルVBA (終点自動)A列を選択するマクロ

エクセルVBAを使ってA列を選択する方法は次の通りです。

終点は自動。始点はA1セルとします。11の例をあげますが、どれも同じ結果になります。

Range(始点, 終点)パターン

Range(Cells(1, "A"), Cells(Cells(Rows.Count, "A").End(xlUp).Row, "A")).Select

Range(Cells(1, 1), Cells(Cells(Rows.Count, 1).End(xlUp).Row, 1)).Select

Range(Cells(1, "A1"), Cells(Rows.Count, "A1").End(xlUp)).Select

Range(Cells(1, 1), Cells(Rows.Count, 1).End(xlUp)).Select

Range(Range("A1"), Cells(Rows.Count, "A1").End(xlUp)).Select

Range(Range("A1"), Cells(Rows.Count, 1).End(xlUp)).Select

Range(Range("A1"), Range("A" & Rows.Count).End(xlUp)).Select

Range(Cells(1, "A1"), Range("A" & Rows.Count).End(xlUp)).Select

Range("始点終点")パターン

Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row).Select

Range("A1:A" & Cells(Rows.Count, 1).End(xlUp).Row).Select

Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row).Select

結果はこうなります。終点はA列の最終行が自動的に選択されます。

1_20201012234001

 

終点をB列に合わせて選択したいときの記述は次の通りです。始点はA1セルとします。次の7つの例は、どれも同じ結果になります。

Range(始点, 終点)パターン

Range(Cells(1, "A"), Cells(Cells(Rows.Count, "B").End(xlUp).Row, "A")).Select

Range(Cells(1, 1), Cells(Cells(Rows.Count, 2).End(xlUp).Row, 1)).Select

Range(Range("A1"), Range("A" & Cells(Rows.Count, "B").End(xlUp).Row)).Select

Range(Range("A1"), Range("A" & Range("B" & Rows.Count).End(xlUp).Row)).Select

Range("始点終点")パターン

Range("A1:A" & Cells(Rows.Count, "B").End(xlUp).Row).Select

Range("A1:A" & Cells(Rows.Count, 2).End(xlUp).Row).Select

Range("A1:A" & Range("B" & Rows.Count).End(xlUp).Row).Select

結果はこうなります。B列の最終行に合わせて選択されます。

2 

 

終点を表の最大行に合わせたい場合は、つぎのとおりです。始点はA1セルとします。

Range("A1:A" & Cells.CurrentRegion.Rows.Count).Select

結果はこうなります。F8に入力がありますのでA列の8行目までが自動選択されます。

3

おすすめ記事

 

« 2020年5月 | トップページ | 2021年10月 »