Help understanding range object

godparticle

New Member
Joined
Feb 2, 2018
Messages
5
I asked something similar a while ago, but could never get a solid answer. Here goes again. I want to understand why and not an alternative solution. Any help would be appreciated.

Suppose I have a 2x2 table with header and want to write to the first row like this:

set rng = listobjects(1).listrows(1).range
rng(2) = "blah"

my questions are:
1. By omitting an argument to rng, am I essentially saying rng( ,2)

2. If i put rng(3), it goes to the next row down. Shouldn't it cause an error if I try to go outside the range specified(range was set to row length of 2)?
Something similar happens if I have no table and set the range like this:

set rng = range("A1:C1")
rng(4) <--- goes to the next row down which is A2
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
23,989
The rng(2) syntax uses an implied .Item property.
I've always found it more clear to explicitly state default properties.
Either

Code:
rng.Cells(2,1).Value = "blah"
' or
rng.Item(2).Value = "blah"
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,730
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
1. No, not really.
2. That's a matter of opinion.

If you only supply one argument, the counting goes from left to right, top to bottom, based on the width of the anchoring range. You'll only get an error if you exceed the number of available cells in a range extending straight down from the anchoring range to the end of the worksheet. So this will work:

Code:
range("A1")(rows.Count)

but this won't:

Code:
range("A2")(rows.Count)
 

godparticle

New Member
Joined
Feb 2, 2018
Messages
5
Thanks for the help. I think I get it. Just trying to wrap my head around some of this. It seems a bit strange how it works. I'll play around with it
 

godparticle

New Member
Joined
Feb 2, 2018
Messages
5
Couldn't see an edit option. That bit about range anchoring is what I was noticing when testing out, but wasn't sure if I was interpreting correctly
 

Watch MrExcel Video

Forum statistics

Threads
1,130,187
Messages
5,640,693
Members
417,161
Latest member
Devon150

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top