0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

VBA学習備忘録⑤ RangeとCellsに関する整理

Last updated at Posted at 2026-01-12

古の時代から何人もの方がまとめている気はするけれども来たるExcel VBAエキスパートのインセンティブに向けて・・

RangeとCellsによるセルの取得方法は次の通り

単一セルの指定

Range("A2")="あああ"
Cells(2,3)="いいい"
Cells(2,"D")="ううう"

セルのアドレスや列記号などは、必ずダブルクォーテーション「"」で囲む

VBE&出力結果

About_Range1.png

VBAではコメントアウトにシングルクォーテーション「'」を利用するため、

Range('A1')

とかしちゃうと普通にコンパイルエラーになる(^^;)

Range("A1")=あああ

とすると 「あああ」はここでは「文字列」ではなく「変数(名)」とみなされる ため、変数定義をしていない場合、コンパイルエラーになる
(⇒ ※「あああ」ではなく、「3」などただの数値であれば、エラーにならずにそのまま代入される)

Cellsは引数が

Cells(行番号,列番号)

の順番であることに注意

『(Rangeの)コロン「:」は引数のセルを頂点とする長方形領域、カンマ「,」はセル・領域の組み合わせ』を表す

Range("A3:C4")="えええ"
Range("A3,C4")="おおお"

後者はA3とC4の2カ所のみに「おおお」の文字列が代入されることに注意

VBE&出力結果

About_Range2.png

以下のように「,(コンマ)」「:(コロン)」は併用もできる

Range("A5,B6:C8") = "かかか"
VBE&出力結果

About_Range3.png

RangeとCellsの併用

次の3つは同義である

Range("A1:C2") = "ききき"
Range(Range("A1"),Range("C2")) = "ききき" 
Range(Cells(1,1),Cells(2,3)) = "ききき"

下2つは「,(コンマ)」を使っているが、下の通り、引数として指定された2つのセルを頂点とする長方形領域が参照される

VBE&出力結果

About_Range4.png

主な参考資料

この1冊で合格! 永井雅明のExcel VBA ベーシック テキスト&問題集

Amazonリンク

https://www.amazon.co.jp/%E3%81%93%E3%81%AE1%E5%86%8A%E3%81%A7%E5%90%88%E6%A0%BC-%E6%B0%B8%E4%BA%95%E9%9B%85%E6%98%8E%E3%81%AEExcel-VBA-%E3%83%99%E3%83%BC%E3%82%B7%E3%83%83%E3%82%AF-%E3%83%86%E3%82%AD%E3%82%B9%E3%83%88/dp/4046071524

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?