View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Bernie Deitrick Bernie Deitrick is offline
external usenet poster
 
Posts: 5,441
Default Strange Range.item behavior?

Jim, and Jeremy,

The arguments are not read right to left, and singel indices are not
referring to rows or columns. (They just seem to sometimes, when working
with single column or single row ranges). Excel steps through rows first
when simply indexing a range.

MsgBox Range("A1:Z10000").Item(100000).Address

works, because the 100000 doesn't refer to a column or a row, but rather the
100000th member when stepping through rowwise.

HTH,
Bernie
MS Excel MVP


"Jim Thomlinson" wrote in message
...
You are only referencing one dimension of a two dimension array. Arguments
are read right to left (backwards from what you would think) so you are
referencing the columns, not the rows... As a general rule you should

alway
reference bot dimensions of the array to avoid any confusion... because it
certainly can be confusing...

HTH

"Jeremy Gollehon" wrote:

Can someone explain this to me?

From the immediate window:
?Range("A1").Item(3,1).address
$A$3
?Range("A1").Item(3).address
$A$3
?Range(Range("A1"), Range("G10")).Item(3,1).address
$A$3
?Range(Range("A1"), Range("G10")).Item(3).address
$C$1 <----!!!???

The last one there is where my confusion lies.

I'm sorting a range with the code below(rough example):


--------------------------------------------------------------------------

-------------------
Set rDataRng = Range(Range("A1"), Range("G10"))
With rDataRng
.Value = .Value
.Sort Key1:=.Item(2, 1), Order1:=xlDescending, OrderCustom:=1, _
Orientation:=xlLeftToRight
End With


--------------------------------------------------------------------------

-------------------

I initially had .Item(2) in there as the Key1 entry, which caused me to
stumble upon this (what I see as) strange behavior.
Is this a bug or one of those "by design" features?

Thanks for any input.
-Jeremy