Thread: Last cell
View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
Paulw2k Paulw2k is offline
external usenet poster
 
Posts: 36
Default Last cell

Hi,

The last cell may not be the one you expect as XL remembers every used cell
so that
the UsedRange may be far larger than you want to print.

The function below returns the 'actual' last cell, given the existing data
spread.

So,

Sub YourProcedure()
.....
Dim rngLastCell as Range
dim sPrintAdrs as String
......

set rngLastCell = RealLastCell(ActiveSheet)

sPrintAdrs = Range("A1:" & rngLastCell.Address)
ActiveSheet.PageSetup.PrintArea = sPrintAdrs

......

End Sub


Function RealLastCell(ws As Worksheet) As Range
Dim LastRow As Long, LastCol As Integer

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

On Error Resume Next

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.
If LastCol = 0 And LastRow = 0 Then
Set RealLastCell = ws.Cells(1, 1)
Else
Set RealLastCell = ws.Cells(LastRow, LastCol)
End If
End Function



Regards

Paul

"Leo Heuser" wrote in message
...
Hi Elsie

One way:

ActiveSheet.Cells.SpecialCells(xlLastCell).Select

--
Best Regards
Leo Heuser

Followup to newsgroup only please.

"Elsie" skrev i en meddelelse
...
Thanks.... but just curious.... how do I select the last cell?

Elsie