Range.Cells vs Range.Index

sijpie

Well-known Member
Joined
Nov 1, 2008
Messages
4,269
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I was digging into VBA to see how I could optimise things, and discovered several ways of accessing cells of a range, two of which seem to be identical. Does someone know the difference?

Code:
Range("A1:G20").Item(2,4)
' is the same as 
Range("A1:G20").Cells(2,4)
'both pointing to cell D2

In fact I think the quickest notation to the top-left cell of a range is:
Code:
Range("MyRange").Item(1)
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Hi sijpie

.Cells gives you the cells in the range, so

Code:
Range("A1:G20")
Range("A1:G20").Cells

are the same thing.

The .Item() property is the default property for the range in this case, so

Code:
Range("A1:G20").Item(2, 4)
Range("A1:G20").Cells.Item(2, 4)

is the same as

Code:
Range("A1:G20")(2, 4)
Range("A1:G20").Cells(2, 4)

Since .Item() is the default property in this case, if you don't want you don't write it, the vba will automatically use it with the range.

Remark: many programmers think that you should always make explicit reference to a property, be it a default property or not.
 
Last edited:
Upvote 0
Thanks for that clear explanantion!
 
Upvote 0

Forum statistics

Threads
1,224,538
Messages
6,179,412
Members
452,912
Latest member
alicemil

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