Thread: Last filled row
View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.misc
Paul Lautman
 
Posts: n/a
Default Last filled row

Alberto Pinto wrote:
Hi!

How can I know what is the last row filled with sometinhg on a page.
I want to get always the last row of a list but that list is variable.

Thanks in advance.


Are you talking in VBA? If so here is a function and a sub:

Sub LastCell()
'Ctrl-L


On Error GoTo blanksheet


Cells(Cells.Find(What:="*", _
SearchDirection:=xlPrevious, _
SearchOrder:=xlByRows).Row, _
Cells.Find(What:="*", _
SearchDirection:=xlPrevious, _
SearchOrder:=xlByColumns).Column).Select
Exit Sub
blanksheet:
Range("A1").Select
End Sub

Function lc(ws As Worksheet) As Range
Dim LastRow&, LastCol%

' Error-handling is here in case there is not any
' data in the worksheet

On Error GoTo blanksheet

With ws

' Find the last real row

LastRow& = .Cells.Find(What:="*", _
SearchDirection:=xlPrevious, _
SearchOrder:=xlByRows).Row

' Find the last real column

LastCol% = .Cells.Find(What:="*", _
SearchDirection:=xlPrevious, _
SearchOrder:=xlByColumns).Column

End With

' Finally, initialize a Range object variable for
' the last populated row.

Set lc = ws.Cells(LastRow&, LastCol%)
Exit Function
blanksheet:
Set lc = ws.Cells(1, 1)

End Function