[VBA Beginner] How to Assign Values to Cells and Ranges | Basics and Usage of the .Value Property

In Excel VBA, you can directly input (assign) values to cells or cell ranges.

Writing values to specific cells is a frequently used operation for initializing forms or as part of data processing.

This article introduces how to assign values to single cells and multiple cells using the .Value property of the Range object.

目次

Entering a String into a Single Cell

Range("B2").Value = "Product Name"

This enters the string “Product Name” into cell B2.

It is also possible to omit .Value and write Range(“B2”) = “Product Name”.

Entering the Same String into Multiple Cells

Range("B4:B6").Value = "In Stock"

The same value “In Stock” is entered into multiple cells (B4, B5, B6) at once.

This functions the same way even if .Value is omitted.

Entering Numbers (Shorthand Notation)

Range("D2") = 150
Range("D4:D6") = 300

For numbers, .Value can also be omitted.

Batch assignment is convenient when you want to input the same number into multiple cells.

Important Note: Setting Different Values in Multiple Cells

Range("E2:E4").Value = Array("A", "B", "C")  ' This will not work as intended

Batch assignment using .Value only works when assigning the same value to all cells.

If you want to set different values, you must use a loop or a two-dimensional array as shown below:

Dim i As Long
For i = 1 To 3
    Range("E1").Offset(i, 0).Value = "Data" & i
Next

Examples of Use Scenes

  • Setting initial values in a form.
  • Inputting headers or constants during data output.
  • Filling values in template automation processes.

Summary

ActionCode Example
Input to Single CellRange("B2").Value = "Product Name"
Same Value to Multiple CellsRange("B4:B6") = "In Stock"
Numeric Assignment (Omitted)Range("D2") = 150
Multiple Different ValuesUse For loops or arrays
よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次