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
| Action | Code Example |
| Input to Single Cell | Range("B2").Value = "Product Name" |
| Same Value to Multiple Cells | Range("B4:B6") = "In Stock" |
| Numeric Assignment (Omitted) | Range("D2") = 150 |
| Multiple Different Values | Use For loops or arrays |
