[Excel VBA] How to Select Cells, Rows, and Columns Relative to a Range

When manipulating table data in VBA, you often need to extract specific cells, rows, or columns from a defined range.

This article explains how to select individual elements from a specific range, such as Range("B2:F4"), using simple code examples.

目次

Specifying a Cell within a Range

The following code selects the cell located in the 2nd row and 1st column within the range “B2:F4” (which corresponds to cell B3).

Range("B2:F4").Cells(2, 1).Select

By specifying the “row and column numbers” relative to the range, you can retrieve the target cell based on its relative position.

Key Points

  • .Cells(Row, Column) refers to the relative position within that specific range.
  • “B2:F4” is a range consisting of 3 rows and 5 columns.
  • Cells(2, 1) points to the 2nd row, 1st column, which is B3.

Getting a Specific Row within a Range

The code below selects the 1st row (B2:F2) within the “B2:F4” range.

Range("B2:F4").Rows(1).Select

Using .Rows(n) allows you to easily retrieve the n-th row relative to the range.

Getting a Specific Column within a Range

The following code selects the 3rd column (Column D) within the “B2:F4” range.

Range("B2:F4").Columns(3).Select

In this case, it targets the “3rd column” inside the specified range, which corresponds to Column D (D2:D4).

Summary: Handling Data via Relative Positions

ActionSample CodeTarget Cells
Get relative cellRange("B2:F4").Cells(2,1)B3
Get relative rowRange("B2:F4").Rows(1)B2:F2
Get relative columnRange("B2:F4").Columns(3)D2:D4

By combining the Range object with Cells, Rows, and Columns, you can flexibly specify positions within any range. This syntax is frequently used in table data processing and loops, so it is a very useful technique to master.

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

この記事を書いた人

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

目次