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

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
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"
 
Upvote 0
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)
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,584
Messages
6,120,387
Members
448,957
Latest member
Hat4Life

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
Back
Top