Excel VBA | 「Cells × Range」実務テンプレ完全版(20パターン)

Excel VBA VBA
スポンサーリンク

Excel VBAでよく使う CellsRange の組み合わせを、実務で役立つ形に整理した「完全版20パターン」です。
コピー&ペーストで即利用できるようにしています。


基本アクセス

  1. 単一セル指定
Range("A1").Value = "Hello"
Cells(1, 1).Value = "Hello"
VB
  1. 行+列で指定
Cells(3, 2).Value = "Data"   'B3セル
VB
  1. 変数で指定
Dim r As Long, c As Long
r = 5: c = 4
Cells(r, c).Value = "変数指定"
VB

範囲指定

  1. 複数セル範囲
Range("A1:C3").Value = "範囲"
VB
  1. Cellsで範囲指定
Range(Cells(1, 1), Cells(3, 3)).Value = "範囲指定"
VB
  1. 変数で範囲指定
Dim r1 As Long, r2 As Long
r1 = 2: r2 = 5
Range(Cells(r1, 1), Cells(r2, 3)).Select
VB

行・列操作

  1. 行全体選択
Rows(3).Select
VB
  1. 列全体選択
Columns("B").Select
VB
  1. Cellsで行範囲指定
Range(Cells(3, 1), Cells(3, 5)).Interior.Color = vbYellow
VB
  1. Cellsで列範囲指定
Range(Cells(1, 2), Cells(10, 2)).Font.Bold = True
VB

動的範囲

  1. 最終行取得
Dim lastRow As Long
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
VB
  1. 最終列取得
Dim lastCol As Long
lastCol = Cells(1, Columns.Count).End(xlToLeft).Column
VB
  1. 最終行まで範囲指定
Range(Cells(2, 1), Cells(lastRow, 3)).Select
VB
  1. 最終列まで範囲指定
Range(Cells(1, 2), Cells(10, lastCol)).Select
VB

コピー&ペースト

  1. 範囲コピー
Range("A1:C3").Copy Destination:=Range("E1")
VB
  1. Cellsでコピー
Range(Cells(1, 1), Cells(3, 3)).Copy Destination:=Cells(1, 5)
VB

ループ処理

  1. 行ループ
Dim i As Long
For i = 1 To lastRow
    Cells(i, 1).Value = i
Next i
VB
  1. 列ループ
Dim j As Long
For j = 1 To lastCol
    Cells(1, j).Value = j
Next j
VB
  1. 二重ループ(表処理)
Dim r As Long, c As Long
For r = 1 To lastRow
    For c = 1 To lastCol
        Cells(r, c).Value = r & "-" & c
    Next c
Next r
VB

応用

  1. 範囲を変数に格納
Dim rng As Range
Set rng = Range(Cells(2, 1), Cells(lastRow, 3))
rng.Interior.Color = vbGreen
VB

実務での使い分けポイント

  • 固定範囲 → Range(“A1:C3”)
  • 動的範囲 → Range(Cells(r1,c1), Cells(r2,c2))
  • 最終行・最終列 → Cells(Rows.Count, col).End(xlUp).Row
  • ループ処理 → Cells(i,j)

💡 この20パターンを押さえておけば、Excel VBAでのセル操作はほぼ網羅できます。

タイトルとURLをコピーしました