Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax to Define an Entire Row as Range

Tags:

excel

vba

In Excel, I can define an entire column as a range and address individual cells in that range like:

Sub headache()
   Dim r As Range
   Set r = Range("A:A")
   MsgBox r(1).Address & vbCrLf & r(2).Address
End Sub

If I try to do the same thing with an entire row:

Sub headache2()
   Dim r As Range
   Set r = Rows(1)
   MsgBox r(1).Address & vbCrLf & r(2).Address
End Sub

I do not get the individual cells in the range. My workaround is:

Set r = Range(Cells(1, 1), Cells(1, Columns.Count))

I can't believe this is the simplest way to make the range...........any suggestions??

like image 963
Gary's Student Avatar asked Jun 03 '15 12:06

Gary's Student


People also ask

How do you define a row range in Excel?

=ROWS(array) The ROWS function uses only one argument: Array (required argument) – This is the reference to a range of cells or array or array formula for which we want the number of rows. The function will give us a numerical value.

How do you reference an entire row in Excel?

If the data in your Excel sheet is organized in rows rather than columns, then you can reference an entire row in your formula. For example, this is how we can calculate an average price in row 2: =AVERAGE($2:$2) - an absolute whole-row reference is locked to a specific row by using the dollar sign ($).

How do you define a range in VBA?

Define the VBA range. The basic syntax of the VBA range property consists of the keyword “Range” followed by the parentheses. The relevant range is included within double quotation marks. For example, the following reference refers to cell C1 in the given worksheet and workbook.

What is a row range?

Answer: It is made up of rows, columns and cells. Rows run horizontally across the worksheet and ranges from 1 to 1048576. A row is identified by the number that is on left side of the row, from where the row originates. Columns run vertically downward across the worksheet and ranges from A to XFD - 1 to 16384.


1 Answers

You can use:

   Set r = Rows(1).Cells

or just use:

   MsgBox r.Cells(1).Address & vbCrLf & r.Cells(2).Address

but note that accessing the Range (or Cells) property of a range using an index will never be restricted to that range alone.

like image 76
Rory Avatar answered Sep 19 '22 16:09

Rory