How to find last selected row in excel worksheet

Posted on 2005-02-25
Medium Priority
Last Modified: 2008-01-09
Is there a way to figure out what is the last row in a selection on a worksheet?  Maybe someone can point me in the right direction.  

So if i select rows 1 to 13, in code i want to figure out that 13 is the last row in the selection.

Is there a way?

Question by:lucas911
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 17

Accepted Solution

aflockhart earned 2000 total points
ID: 13404538
in VB code ?

selection.row     tells you the first row selected

selection.rows.count   tells you how many are selected


selection.row + selection.rows.count -1  

tells you the last row
LVL 16

Expert Comment

by:Calvin Brine
ID: 13404557
Another option.

Dim R as String

r = range("A65535").end(xlup).row

Will return the last used row in Column A.

LVL 13

Author Comment

ID: 13404723
Exactly what i was looking for THANKS A MILLION!
LVL 33

Expert Comment

by:Jeroen Rosink
ID: 13404763
here some other macro's to find the last anything:

'Find the last used cell, before a blank in a Column:
Sub LastCellBeforeBlankInColumn()
   MsgBox Range("A1").End(xlDown).Address
End Sub

'Find the very last used cell in a Column:
Sub LastCellInColumn()
   MsgBox Range("A65536").End(xlUp).Address
End Sub

'Find the last cell, before a blank in a Row:
Sub LastCellBeforeBlankInRow()
    MsgBox Range("A1").End(xlToRight).Address
End Sub

'Find the very last used cell in a Row:
Sub LastCellInRow()
   MsgBox Range("IV1").End(xlToLeft).Address
End Sub

'Find the very last used cell on a Worksheet:
Sub Demo()
   MsgBox Cells.Find(What:="*", After:=[A1], SearchDirection:=xlPrevious).Address
End Sub

Sub NumberOfUsedRowsInA()
MsgBox RowsA = Cells(Rows.Count, 1).End(xlUp).Row
End Sub

or for the last row number in the selection

Sub LastRowInSelection()
For Each cel In Selection
    LastRow = cel.Row
Next cel
MsgBox LastRow
End Sub


LVL 33

Expert Comment

by:Jeroen Rosink
ID: 13404882
keep in mind that the line: selection.rows.count shows only the number of rows which are selected. not the number of the last row which is selected


Featured Post

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Cancel future meetings from user mailboxes in Office 365 using Remove-CalendarEvents
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question