ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   selection.cells(1).value (https://www.excelbanter.com/excel-programming/415198-selection-cells-1-value.html)

greg

selection.cells(1).value
 
Hello,
I am working with the selection object.

If I select a single cell. my selection.Cells.Count = 1.

I can do this and get the correct value of the cell:
selection.cells(1).value

However I can also do this:
selection.cells(0).value
and get the value of the cell above the selected.
Is this correct? Why is this happening?
Seems a little scary?




joel

selection.cells(1).value
 
Line 1 and 2 are equivalent

selection.cells(1).value

cells(rowoffset:=1,columnoffset:=0)


so what you are saying is Range("A1").cells.count = Range("A2").cells.count
= 1


"greg" wrote:

Hello,
I am working with the selection object.

If I select a single cell. my selection.Cells.Count = 1.

I can do this and get the correct value of the cell:
selection.cells(1).value

However I can also do this:
selection.cells(0).value
and get the value of the cell above the selected.
Is this correct? Why is this happening?
Seems a little scary?





Jim Thomlinson

selection.cells(1).value
 
Although it is interesting that you can read beyond the boundaries of the
range object it is not exactly a fatal flaw. The reason that this is
happening is that a range object is essentially just a pointer to the first
cell with a reference to the number of cells that encompass the range. If you
offset from the pointer outside of the valid range of the object then you get
into cells that are not part of the range object. Very similar to C/C++
programming where you can read or write beyond the boundaries of an array or
such.
--
HTH...

Jim Thomlinson


"greg" wrote:

Hello,
I am working with the selection object.

If I select a single cell. my selection.Cells.Count = 1.

I can do this and get the correct value of the cell:
selection.cells(1).value

However I can also do this:
selection.cells(0).value
and get the value of the cell above the selected.
Is this correct? Why is this happening?
Seems a little scary?






All times are GMT +1. The time now is 06:11 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com