View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
GS[_2_] GS[_2_] is offline
external usenet poster
 
Posts: 3,514
Default lastrow function help

Thought this might make for better code brevity...


Public Function GetLastDataPos&(Wks As Worksheet, _
IsRow As Boolean, Optional StartPos& = 1)
' Finds the last row or col of UsedRange that contains data.
' Allows for excluding any number of header rows/cols.
'
' Args:
' Wks Object ref to the sheet being searched.
'
' IsRow Boolean value that determines which axis to test.
'
' StartPos Optional:
' A type Long that specifies the start row/col of the
search.
' If omitted the search starts at A1.
'

Dim n&, k&, lLast&
With Wks.UsedRange
lLast = IIf(IsRow, .Rows.Count, .Columns.Count)
For n = lLast To StartPos Step -1
k = Application.CountA(IIf(IsRow, Wks.Rows(n), Wks.Columns(n)))
If k 0 Then GetLastDataPos = n: Exit Function
Next 'n
End With 'Wks.UsedRange
End Function

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion