PDA

View Full Version : Solved: ?Cells? and ?Range? Properties



Cyberdude
06-09-2007, 12:47 PM
I rarely use the Cells property, so when I just saw these two statements, I became confused:

Range(?A1:A2?) = ?XYZ?
Range(Cells(1,1), Cells(2,1)) = ?XYZ?

I wondered why it is that in the first statement the range limits are separated by a colon (:), but in the second statement a comma (,) is used.

Bob Phillips
06-09-2007, 02:37 PM
The colon separator is a way of specifying a block of cells, but you could also use Range("A1","A2").

Cells is an object property of Range, and the , is required to separate the Range arguments. If you used :, then it would all be treated as the first argument, and VBA just can't resolve that expression.

malik641
06-09-2007, 06:28 PM
You could also use Range("A1, A2"). And use Range("A1:A2, B3:B6") to define multiple blocks of ranges. I would guess it reflects the usage of Excel's formulas to define ranges. From what I understand, using "A1:A3" tells VBA that you're giving the range a string argument and therefore will treat it the same as using it in a SUM formula, for example. If I had to guess, I would say the Range class uses an overloading method because of the fact you can give it a range object (Cells()) or a string parameter. But this is a guess, I'm not sure if I'm right.

Cyberdude
06-10-2007, 03:00 PM
Thanks, guys ... just trying to understand and you did a good job of explaining. :friends:
Sid

malik641
06-10-2007, 04:01 PM
Glad to help :friends: