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

Oops! I've never run the following col function and so I just now
noticed my copy/paste booboo on the inner loop. Here's the revised
(actually works) version...

Public Function GetLastDataCol&(Wks As Worksheet, Optional StartCol&,
Optional StartRow&)
' Finds the last col of UsedRange that contains data.
' Allows for excluding any number of header rows/cols.
'
' Args:
' Wks Object ref to the sheet being searched.
' StartCol Optional:
' A type Long that specifies the start col of the search.
' If omitted the search starts at Col1.
' StartRow Optional:
' A type Long that specifies the start row of the search.
' If omitted the search starts at Row1.
'

Dim vData, n&, k&, lRow&, lCol&

vData = Wks.UsedRange '//load the data
lCol = IIf(StartCol 0, StartCol, 1) '//get 1st col
lRow = IIf(StartRow 0, StartRow, 1) '//get 1st row

For n = UBound(vData, 2) To lCol Step -1
For k = lRow To UBound(vData)
If Len(vData(n, k)) 0 Then GetLastDataCol = n: Exit For
Next 'k
Next 'n
End Function

I have used the row function, though, primarily to eliminate looping
the extra/blank rows included in UsedRange!

You might be interested in...
Spread defines ranges by cell positions in the sheet (grid) as
follows...

With fpSpread1
.Sheet = .ActiveSheet
.Row = <1st row#
.Row2 = <last row#
.Col = <1st col#
.Col2 = <last col#
End With

...and so these go with all the range functions/methods. Thus, I've
stopped using...

Dim lLastRow& lLastCol&

in favor of using...

Dim lRow2&, lCol2&

...to cut down on my typing difficulties due to having Lou Gehrig's.
This is also why I've adopted using VB's type symbols. The advantage is
my code's brevity and easier maintenance.

--
Garry

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