Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 279
Default for each in a range

I run Excel 2003 and VBA 6.5.

I have a very limited understanding of VBA's Range object.

I hit this on a sheet which has relevant formulae in column C.
I want to treat the first formula as special.
I have the following code extract:
....
Dim PeakOyster As Boolean
Dim Trips As Range
....
Set Trips = Columns(3).SpecialCells(xlCellTypeFormulas)
PeakOyster = (Trips.Item(0)(1, -1) < 930 And Left(Cells(2, 2), 1) < "S")
For Each C In Trips
....

Trips.Item(0) is evaluated without error.
This surprises me as Item is not shown as a member of Trips in the VBA
Locals Window, nor is it a documented member of the Range object.

The first formula is in $C$9.
However the Immediates Window shows
?trips.Item(0).address
$C$8
?trips.Address
$C$9:$C$10,$C$15,$C$18,$C$24,$C$26,$C$33,$C$35,$C$ 41,$C$43,$C$49,$C$51,$C$57:$C$58
?trips(1,-1).address
$A$9

i.e. trips.Item(0).address is $C$8, where I expect $C$9.

I can correct my code to
PeakOyster = (Trips(1, -1) < 930 And Left(Cells(2, 2), 1) < "S")

I don't understand the logic of what I see.
I would value an explanation and a reference to documentation.
I'll even buy a book, if necessary. ;)

P.S. Since starting to write this, I have found
?trips.Item(1).address
$C$9
i.e. Item is equivalent to a base 1 array and
for each Q in Columns(3).SpecialCells(xlCellTypeFormulas):? Q.address:next
$C$9
$C$10
$C$15
....

My code now contains:
' Trips.Item is absent from the Locals Window and is 1-based
PeakOyster = Trips.Item(1)(1, -1) < 930 And Not Cells(2, 2) Like "S*"
--
Walter Briscoe
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default for each in a range

Just a hint on how Excel and VBA manage ranges: There is never a zero
cell! Item(0) is 1 less than Item(1) and so if $C$9 is the 1st cell
(ie: Trips.Cells(1).Address) then it makes sense that Item(0) (ie:
Trips.Cells(-1).Address) is evaluated as $C$8!

When you dump a range into a variant you result a 2D array that is 1
based...

Dim vData As Variant
vData = Range("$C$9:$C$58")

...which results to vData having 50 rows and 1 column, which is the same
as...

Dim vData(1 To 50, 1 To 1)

...because there is no Row0 or Col0. Had your range started at $C$1
instead of $C$9 then Item(0) would either evaluate to ref the last cell
in ColB --OR-- throw an exception.

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I enter formula sum(range+range)*0.15 sumif(range=3) tkw Excel Discussion (Misc queries) 2 October 1st 09 09:17 PM
Excel Addin:Setting the range to the Excel.Range object range prop Rp007 Excel Worksheet Functions 5 November 24th 06 04:30 PM
Range Question / error 1004: method Range of object Worksheet has failed Paul Excel Programming 3 April 7th 05 02:56 PM
Range.Find returns cell outside of range when range set to single cell Frank Jones Excel Programming 12 June 10th 04 04:22 AM
how to? set my range= my UDF argument (range vs. value in range) [advanced?] Keith R[_3_] Excel Programming 2 August 11th 03 05:55 PM


All times are GMT +1. The time now is 04:46 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"