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?

More than 1 year has passed since last update.

Excel VBA 行列どっち?

Posted at

初めに

VBAを書くときにいつも行列がどっちかを忘れてしまうのでまとめました。

そもそも行列どっち?

そもそも行列がどっちかを忘れてしまった人もいるかと思いますので以下にまとめます。(投稿者のことです( ´∀` ))

行列どっち
行:縦    # 「ぎ」をひらがなで書くときに1書目が横だから
列:横    # 「れ」をひらがなで書くときに1書目が縦だから
Row : 行    # 「R」の1書目と2書目の終点を結ぶ横方向だから
Column : 列 # 「C」の1始点と終点を結ぶと縦方向だから

上記の内容からエクセルだと以下のようになります。

エクセルの場合行列どっち
数字:行(Row)
アルファベット:列(Column)

実際エクセルで表すと以下のようになります。
image.png

セルの値を取得

実際にセルの値を取得してみます。
以下のような値を取得することを想定します。
image.png
まずは、「Range」で使用した場合は以下のコードになります。

Rangeを使用した場合
Sub GetRowColmunValue_Btn()
    Debug.Print (Range("A1").Value)
    Debug.Print (Range("A2").Value)
    Debug.Print (Range("A3").Value)
    Debug.Print (Range("A4").Value)
    Debug.Print (Range("A5").Value)
    Debug.Print (Range("B1").Value)
    Debug.Print (Range("B2").Value)
    Debug.Print (Range("B3").Value)
    Debug.Print (Range("B4").Value)
    Debug.Print (Range("B5").Value)
    Debug.Print (Range("C1").Value)
    Debug.Print (Range("C2").Value)
    Debug.Print (Range("C3").Value)
    Debug.Print (Range("C4").Value)
    Debug.Print (Range("C5").Value)
End Sub

上記の実行結果です。

実行結果
1行目A列
2行目A列
3行目A列
4行目A列
5行目A列
1行目B列
2行目B列
3行目B列
4行目B列
5行目B列
1行目C列
2行目C列
3行目C列
4行目C列
5行目C列

Cellsを使用する場合は、以下の書き方です。

Cells(行,列).value

Rangeと同じ場所を取得する場合は以下のように書きます。

Cellsを使用した場合の書き方
Sub GetRowColmunValue_Btn()
     'A1
     Debug.Print (Cells(1, 1).Value)
     'A2
     Debug.Print (Cells(2, 1).Value)
     'A3
     Debug.Print (Cells(3, 1).Value)
     'A4
     Debug.Print (Cells(4, 1).Value)
     'A5
     Debug.Print (Cells(5, 1).Value)
     'B1
     Debug.Print (Cells(1, 2).Value)
     'B2
     Debug.Print (Cells(2, 2).Value)
     'B3
     Debug.Print (Cells(3, 2).Value)
     'B4
     Debug.Print (Cells(4, 2).Value)
     'B5
     Debug.Print (Cells(5, 2).Value)
     'C1
     Debug.Print (Cells(1, 3).Value)
     'C2
     Debug.Print (Cells(2, 3).Value)
     'C3
     Debug.Print (Cells(3, 3).Value)
     'C4
     Debug.Print (Cells(4, 3).Value)
     'C5
     Debug.Print (Cells(5, 3).Value)
End Sub

上記の実行結果です。

実行結果
1行目A列
2行目A列
3行目A列
4行目A列
5行目A列
1行目B列
2行目B列
3行目B列
4行目B列
5行目B列
1行目C列
2行目C列
3行目C列
4行目C列
5行目C列

最後に

ここまで見ていただきありがとうございます。

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?