Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 265
Default how to get the last range address (not the last cell)

Hi, I found several posts indicating how to get the last cell, however I
would like to get the last selected range,

ej.

I select range A1:F2, then I select cell J3, I would like to get a variable
with the A1:F2 address after I selected the cell J3.

Thanks
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default how to get the last range address (not the last cell)

Hi,

Try this. You don't of course need the message box that's just to
demonstrate it's working. Right click your sheet tab, view code and paste the
code in

Private lastRange As Range

Private Sub Worksheet_Activate()
Set lastRange = ActiveCell
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error Resume Next
MsgBox "Last selected cell was " & lastRange.Address & Chr(10) _
& "Current cell is " & Target.Address
Set lastRange = Target
End Sub


Mike

"filo666" wrote:

Hi, I found several posts indicating how to get the last cell, however I
would like to get the last selected range,

ej.

I select range A1:F2, then I select cell J3, I would like to get a variable
with the A1:F2 address after I selected the cell J3.

Thanks

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default how to get the last range address (not the last cell)

I should have added that you then go to your worksheet and select cells,
multiple cell ranges or non contiguous ranges by holding down the CTRL key

Mike

Mike

"Mike H" wrote:

Hi,

Try this. You don't of course need the message box that's just to
demonstrate it's working. Right click your sheet tab, view code and paste the
code in

Private lastRange As Range

Private Sub Worksheet_Activate()
Set lastRange = ActiveCell
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error Resume Next
MsgBox "Last selected cell was " & lastRange.Address & Chr(10) _
& "Current cell is " & Target.Address
Set lastRange = Target
End Sub


Mike

"filo666" wrote:

Hi, I found several posts indicating how to get the last cell, however I
would like to get the last selected range,

ej.

I select range A1:F2, then I select cell J3, I would like to get a variable
with the A1:F2 address after I selected the cell J3.

Thanks

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default how to get the last range address (not the last cell)

You could store current/previous selections as Range objects, Named ranges,
or string address. There are pro/con's with each method. An example storing
the range objects

' code in Thisworkbook module

Private Sub Workbook_Activate()
If TypeName(Selection) = "Range" Then
SetSel Selection
End If
End Sub

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target
As Range)

SetSel Target

End Sub

' code in a normal module
Public grLastSel As Range, grCurSel As Range

Sub SetSel(rSel As Range)

Set grLastSel = grCurSel
Set grCurSel = rSel

''' depending on what you want, maybe
If grLastSel Is Nothing Then
Set grLastSel = rSel
End If

End Sub

Sub abc()
msgbox grLastSel.Parent.Name & vbCr & grLastSel.Address(0, 0)
End Sub

You'll need to initialise things. Note range objects (& named ranges) can
become corrupted if entire rows/cols are deleted from any area. OTOH, string
address is limited to 255 and is "historical".

Regards,
Peter T


"filo666" wrote in message
...
Hi, I found several posts indicating how to get the last cell, however I
would like to get the last selected range,

ej.

I select range A1:F2, then I select cell J3, I would like to get a
variable
with the A1:F2 address after I selected the cell J3.

Thanks



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
cell address for first value to appear in a range Dave F[_2_] Excel Discussion (Misc queries) 5 March 19th 08 05:51 PM
Cell address in a range starguy Excel Discussion (Misc queries) 7 May 3rd 06 11:58 AM
cell address rather than range name Angi Bemiss Excel Discussion (Misc queries) 1 December 1st 05 12:46 AM
HOW TO: Get cell address from a range Bob Phillips[_6_] Excel Programming 0 May 4th 04 04:08 PM
HOW TO: Get cell address from a range Juan Pablo González Excel Programming 0 May 4th 04 03:42 PM


All times are GMT +1. The time now is 01:54 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"