0
1

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 3 years have passed since last update.

【VBA】セルへの値代入と削除

Last updated at Posted at 2021-04-19

◆セルへの値の代入

rangeSample.xlsm
Sub rangeSample()

    'セルへの値の代入
    Worksheets("Sheet1").Range("A1").Value = "A1"
    
    'Worksheets を省略するとアクティブなシートが対象となり動作する
    Range("A2").Value = "A2"
    
    ' 複数セルの への代入
    Range("A3", "B4").Value = "A3 - B4"
    Range("A5:B6").Value = "A5 - B6"

     '行や列全体への代入
    Range("7:7").Value = "Row 7"
    Range("C:C").Value = "Column C"
      
End Sub
cellsSample.xlsm
Sub cellsSample()

    'Cells を用いると行・列を数値で指定できる
    Worksheets("Sheet1").Cells(1, 5).Value = "(1, 5)"
    Cells(2, 5).Value = "(2, 5)"
    
End Sub

◆相対位置のセルに値を代入

offsetSample.xlsm
Sub offsetSample()

   'Offset を用いると相対位置に値を入力できる
    Range("A1").Offset(1, 1).Value = "A1 + (1, 1)"
    Cells(3, 3).Offset(1, 1).Value = "(3, 3) + (1, 1)"

End Sub

◆セルの値の削除

clearSample.xlsm
Sub clearSample()

    Range("A1:C3") = 1

    'Clear で値を削除できる
    Range("A1").Clear
    Cells(2, 2).Clear

End Sub

なお、Cells.Clearをすれば、全セルの値を削除する。

0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?