View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Neal Zimm Neal Zimm is offline
external usenet poster
 
Posts: 345
Default UsedRange and LastCell feb08

Jim,
Thanks much.
To be on the lookout, can you give me a circumstance or two when the MSoft
supplied results are wrong?

Also, as a matter of good coding practices, do you always use so many
continuation lines, or is your code continued just for this community for
readability?

Thanks again,
--
Neal Z


"Jim Thomlinson" wrote:

Purely as an aside to your question both used range and last cell can at
times be wrong. Most of the time they are correct but there will be times
when they get messed up. I personally never use them for that reason (I like
to be right all of the time. My wife will attest to that). I use the
following function to return the last cell. It never fails...

Public Function LastCell(Optional ByVal wks As Worksheet) As Range
Dim lngLastRow As Long
Dim intLastColumn As Integer

If wks Is Nothing Then Set wks = ActiveSheet
On Error Resume Next
lngLastRow = wks.Cells.Find(What:="*", _
After:=wks.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
intLastColumn = wks.Cells.Find(What:="*", _
After:=wks.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
On Error GoTo 0
If lngLastRow = 0 Then
lngLastRow = 1
intLastColumn = 1
End If
Set LastCell = wks.Cells(lngLastRow, intLastColumn)

End Function
--
HTH...

Jim Thomlinson


"Neal Zimm" wrote:

Hi -
Am learning about some new to me worksheet properties.
I know the first example below is a count, but shouldn't the row numbers
be the same in both examples? Why are they not?
Thanks,
Neal Z.


debug.Print activesheet.usedrange.rows.count
5499

debug.Print activesheet.usedrange.specialcells(xlCellTypeLastC ell).address
$CW$5498