View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Tom Ogilvy Tom Ogilvy is offline
external usenet poster
 
Posts: 27,285
Default Weird range property behaviour

Sub Temp()
Dim rng0 As Range, rng1 As Range, y As Integer
y = 3

Set rng0 = Worksheets(1).Cells(y, 1)
Set rng1 = Range(rng0(1), rng0(2))

Debug.Print "Top row rng0: " & rng0.Row
Debug.Print "Top row rng1: " & rng1.Row

End Sub

will give you the same top row.

Range(rng0(1), rng0(2))
is relative to the top left cell of the spread sheet.

rng0.Range(rng0(1), rng0(2))
is relative to the location of rng0

rng0 is A3
rng0(1) is A3
rng0(2) is A4

frm the immediate window:
set rng0 = cells(3,1)
? rng0.address, rng0(1).address, rng0(2).address
$A$3 $A$3 $A$4

Range("A3","A4") refers to A3:A4 - the addresses are absolute/relative to
A1

rng0.Range("A3","A4") says, from A3, step down 3 rows (counting A3 as the
first) and address a two cell vertical range.

--
Regards,
Tom Ogilvy



"Gareth Thackeray" wrote in message
...
Hi Tom,

Thanks for the help Tom, I used Resize instead to accomplish what I wanted

I still don't quite understand what happens when you use rng.Range(cell1,
cell2). For your interest, a sub explaining my confusion is below:

Sub Temp()
Dim rng0 As Range, rng1 As Range, y As Integer
y = 3

Set rng0 = Worksheets(1).Cells(y, 1)
Set rng1 = rng0.Range(rng0(1), rng0(2))

Debug.Print "Top row rng0: " & rng0.row
Debug.Print "Top row rng1: " & rng1.row

End Sub

I would expect the top row in either case to be y, but instead the top row
of rng1 always seems to be 2y -1.

FYI, what I'm actually trying to do is to get a range that consists of

rows
r0 to r1 of an existing range.

Best regards,

Gareth