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の可読性を高める!配列を使ったイケてるデータ書き込み術

Last updated at Posted at 2025-10-30

EXCELのまっさらなシートに以下のようなデータを書き込んで行くとします

image.png

一般的に思いつく方法

Sub Test1()
    Dim i, 名前, 年齢, 性別
    
    Cells.ClearContents ' シートをクリア
    
    ' ヘッダー行を書き込み
    Cells(1, "A").Value = "名前"
    Cells(1, "B").Value = "年齢"
    Cells(1, "C").Value = "性別"
    
    For i = 2 To 2 ' 明細書き込み
        ' 実際は色々処理して変数を取得
        名前 = "山田"
        年齢 = 20
        性別 = "男"
        
        ' 明細を1セルずつ書き込み
        Cells(i, "A").Value = 名前
        Cells(i, "B").Value = 年齢
        Cells(i, "C").Value = 性別
    Next
End Sub

今回紹介する方法

Sub Test2()
    Dim i, 名前, 年齢, 性別
    
    Cells.ClearContents ' シートをクリア
    
    ' ヘッダー行を一括で書き込み
    Range("A1:C1").Value = Array("名前", "年齢", "性別")
    
    For i = 2 To 2 ' 明細書き込み
        ' 実際は色々処理して変数を取得
        名前 = "山田"
        年齢 = 20
        性別 = "男"
        
        ' 明細行を1行単位で一括書き込み!
        Range("A:C").Rows(i).Value = Array(名前, 年齢, 性別)
    Next
End Sub

Range("A1:C1").Value = Array("名前", "年齢", "性別")
どのセル範囲「Range("A1:C1")」にどんな見出し「("名前", "年齢", "性別")」を設定するのか一目瞭然です

Range("A:C").Rows(i).Value = Array(名前, 年齢, 性別)
どの列「Range("A:C")」のどの行「Rows(i)」にどの値「(名前, 年齢, 性別)」を設定するのか一目瞭然です

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?