accessing last cell in selection on sheet

  • Thread starter Thread starter gnosis
  • Start date Start date
G

gnosis

Well, i'm writing an application in delphi and i use XLSContainer to
access the cells in an xls document.
I need to find a method that would help me to get the
coordinates(column,row) of the last selected cell in the selection.

so far found this methods:
XLSContainer1.Application.ActiveCell.Column
XLSContainer1.Application.ActiveCell.Row
XLSContainer1.Application.ActiveWindow.RangeSelection.Columns.Count
XLSContainer1.Application.ActiveWindow.RangeSelection.Rows.Count
XLSContainer1.Application.Range[str,str].Activate

With these methods i've managed to access all cells but only when the
selction goes
from left to right and from up to down....If i could get he
coordinates(column,row) of the last selected cell in the selection i
will solve my problem!

Hope that you understand what i wrote:)
koko
 
If I understand correctly, the last selected cell should
be the active cell in which case u could simply get the
address.

dim a as string
a= activecell.address
-----Original Message-----

Well, i'm writing an application in delphi and i use XLSContainer to
access the cells in an xls document.
I need to find a method that would help me to get the
coordinates(column,row) of the last selected cell in the selection.

so far found this methods:
XLSContainer1.Application.ActiveCell.Column
XLSContainer1.Application.ActiveCell.Row
XLSContainer1.Application.ActiveWindow.RangeSelection.Colu mns.Count
XLSContainer1.Application.ActiveWindow.RangeSelection.Rows
..Count
XLSContainer1.Application.Range[str,str].Activate

With these methods i've managed to access all cells but only when the
selction goes
from left to right and from up to down....If i could get he
coordinates(column,row) of the last selected cell in the selection i
will solve my problem!

Hope that you understand what i wrote:)
koko


--
gnosis
---------------------------------------------------------- --------------
gnosis's Profile: http://www.excelforum.com/member.php? action=getinfo&userid=16806
View this thread: http://www.excelforum.com/showthread.php?threadid=320055

.
 
Well, by last cell i mean the most distant from the firs
selected(ActiveCell) in the selection..that's the problem!
For the ActiveCell is easy:
diXLSContainer1.Application.ActiveCell.Column;
diXLSContainer1.Application.ActiveCell.Row;

How can i get these values for the most distant cell in the selection
not the ActiveCell
 
Not sure, but expect you will have to track and capture
the mousemove events
 
Is there any reason you cannot just select the last cell in th
selection, grab the address, then re-select the original selection?
for example:

RA = Selection.Address ' curren
selection

Selection.End(xlDown).Select 'select last cell
ActiveCell.Offset(-1, 0).Range("A1").Select

topaddressrow1 = ActiveCell.Row 'grab addresses o
last cell
topaddresscolumn1 = ActiveCell.Column

Range(RA).Select 'reselec
original selection

' may be a work around - but hope it helps.
FuzzyDov
 
Perhaps I'm way off, but if you really want the row and column values
for the cell the farthest away from the active cell in any selection
(contiguous or not), then try something like this. R and C return the
row and column values in this example. This is not fully tested, but
seems to accomplish what you requested.


Code:
--------------------
Sub Test()
Dim Rng As Range
Dim R As Long
Dim C As Long
Dim RowDif As Long
Dim ColDif As Long
RowDif = 0
ColDif = 0
For Each Rng In Selection
If Abs(ActiveCell.Row - Rng.Row) > RowDif Or Abs(ActiveCell.Column - Rng.Column) > ColDif Then
RowDif = Abs(ActiveCell.Row - Rng.Row)
ColDif = Abs(ActiveCell.Column - Rng.Column)
R = Rng.Row
C = Rng.Column
End If
Next Rng
Debug.Print R; C
End Sub
 
If I have A2 and B1 selected, which is the cell you are looking for?

In otherwords, for a single rectangular selection, the last cell is the
lower right cell, but you say you have no problem getting that - thus you
problem is with a multi area selection and in that case, it is not clear
what constitutes the last cell. What are your rules.
 
Sorry if i was not clear..I'm talking about a single selection, wit
only one stroke, one rectangle...
actually i'm woking on delphi application and i use a XLSContaine
component with all objects,methods and properties fro
EXCEL-VBA...XLSContainer displays some EXCEL file that is loaded o
FormShow...
..finnaly i need to access all the cells from a single selection mad
by a client(by some buttonclick-event or whatever) and put the value
from the cells in a database..
So far i manage to get the number of columns and rows in the selectio
by these methods:
XLSContainer1.Application.ActiveWindow.RangeSelect ion.Columns.Count
XLSContainer1.Application.ActiveWindow.RangeSelect ion.Rows.Count

and coordinates of the first cell in the selection
XLSContainer1.Application.ActiveCell.Column
XLSContainer1.Application.ActiveCell.Row

the problem is that i cannot predict the direction of the selection:
there are four possibilities:
- to select from left to right,up to down
- to select from rigt to left, up to down
- to select from left to right, down to up
- to select from right to left, down to up
if i could get the coordinates of the most distant cell from the firs
activCell in the selection i will know about the direction of th
movement of the client.
I can get these values in VB editor with some
VB macro like
If TypeName(Selection) <> "Range" Then Exit Sub
For Each cell In Selection

cell.Activate
...some code
Next cell
but it's usles in delphi
if anybody have an idea i would be greatfull
Thanx so far!!
 
In VBA it would be

Selection.Item(selection.Count)

from the immediate window:

? selection.Address
$C$6:$F$16
? selection.Item(selection.count).Address
$F$16


for a single range/area, the selection or range will always be left to right
and top to bottom, regardless of how the user made the selection.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top