[VBA for Beginners] How to Get Cells by Address, Row, and Column Numbers

In VBA (Visual Basic for Applications), manipulating cells is a frequent task. Knowing how to retrieve cells using both Cell Addresses (e.g., A1) and Row/Column Numbers (e.g., Row 1, Column 6) is extremely useful.

In this article, I will explain the standard methods for retrieving cells.

目次

Specifying Cells by Address (Range)

The most basic method to specify an Excel cell in VBA is using Range.

Range("A1").Value = "Cell A1"

This code enters the string “Cell A1” into cell A1.

Specifying a Range by Address

If you want to specify multiple cells at once, you can also use Range.

Range("A3:B5").Value = "Cell A3:B5"

In this code, the value “Cell A3:B5” is entered into the range from A3 to B5 (a total of 6 cells) all at once.

Specifying a Range Using Start and End Cells

You can also specify a range by enclosing the starting cell and ending cell within Range.

Range(Range("C1"), Range("D10")).Value = "C1 to D10"

This syntax is convenient when the start or end positions are determined dynamically, such as when processing based on a user’s selected cell range.

Specifying Cells by Row and Column Numbers (Cells)

VBA provides the Cells property to specify cells using row and column numbers.

' Syntax: Cells(RowNumber, ColumnNumber)
Cells(1, 6).Value = "Row 1, Col 6"

In this example, “Row 1, Col 6” is entered into the 1st row and 6th column (which is cell F1).

Since columns can be specified by numbers instead of letters, this method works very well with loops (e.g., For loops) and is highly practical for automated processing.

Specifying Columns by Letter

With Cells, it is also possible to specify the column using a letter (string). In this case, treat the letter as a string, like "F".

Cells(3, "F").Value = "Row 3, Col F"

In this example, the value is set in the 3rd row, column F. This is useful when you want to clearly specify a column or prioritize readability.

Summary

There are two main ways to specify cells:

MethodUsage ExampleDescription
RangeRange("A1")Directly specifies a specific cell.
Range("A3:B5")Explicitly specifies a range.
Range(Range("C1"), Range("D10"))Specifies start and end cells separately.
CellsCells(1, 6)Specifies by Row/Column number (Column is numeric).
Cells(3, "F")Specifies by Row number/Column letter (Column is string).

By distinguishing between these methods depending on the situation, you can write more flexible and efficient VBA code.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

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

目次