View Single Post
  #10   Report Post  
Posted to microsoft.public.excel.programming
Doug Glancy Doug Glancy is offline
external usenet poster
 
Posts: 770
Default Activate last cell in selected range - an example

So here's my stab at it. I've assumed that the definition of the last cell
is whichever is farthest from A1, i.e., total rows + columns. In some cases
it matches Tom's, in others not (Tom's seems to favor columns). If it comes
up a tie it goes with the earlier solution:

Sub test()

Dim i As Long, area_distance_from_A1 As Long
Dim longest_distance As Long, last_area As Long

With Selection
For i = 1 To .Areas.Count
With .Areas(i)
Set area_last_cell = Range("A1:" & .Cells(.Cells.Count).Address)
area_distance_from_A1 = area_last_cell.Rows.Count +
area_last_cell.Columns.Count
If area_distance_from_A1 longest_distance Then
longest_distance = area_distance_from_A1
last_area = i
End If
End With
Next i
End With

With Selection.Areas(last_area)
.Cells(.Cells.Count).Activate
End With

End Sub

Thanks to DataFreak for an interesting problem,

Doug

"DataFreakFromUtah" wrote in message
om...
No question here, just a procedure for the archive.

Search critera: activate the last cell in a selection
select last cell in range
select last cell in selection
activate last cell in range


Sub SelectActivateLastCellInSelectedRange()
'Activates the last cell in the selected range but keeps the
'same range selected


Dim LastRow As Variant
Dim LastCol As Variant
Dim TempRow As Variant
Dim TempCol As Variant
Dim LastCell As Range
Dim A As Range


LastRow = 1
LastCol = 1
ActiveWorkbook.Activate
For Each A In Selection.Areas
TempRow = A.Range("A1").Offset(A.Rows.Count - 1, 0).Row
If TempRow LastRow Then LastRow = TempRow
TempCol = A.Range("A1").Offset(0, A.Columns.Count - 1).Column
If TempCol LastCol Then LastCol = TempCol
Next A
Set LastCell = Cells(LastRow, LastCol)
LastCell.Activate


End Sub