View Single Post
  #7   Report Post  
Posted to microsoft.public.excel.programming
michael.beckinsale michael.beckinsale is offline
external usenet poster
 
Posts: 274
Default Help to improve UDF please

Hi All,

Many thanks for all your input. I have taken on board your comments
and made some amendments.

My aim was to build 1 'universal' intuitive function that could be
used anywhere in the coding of a project to return the last row or
last column and in which if using the optional parameters could be
more specific. Additionally when reviewing the project code it should
be obvious what is being done.

I have decided that it is much better and intuitive to split this
'universal' function into a function each for column & row.

Peter - Your comment re making MyCol a variant as opposed to a string
is noted and would make the function more flexible but l feel that
generally referring to a column by its alpha character makes the code
more 'readable'.

I was thinking of adding an additional argument for a 'workbook' if
for instance one needed to refer to arange in an unknown range in an
external workbook. Your thoughts would be appreciated.

Any comments on improvement welcomed.

Here is the code:

'---------------------------------------------------------------------------------------
' Procedure : LR
' DateTime : 07/11/07 11:43
' Author : Michael Beckinsale
' Purpose : To get the last row number. Used without the optional
parameters the last
' row anywhere on the ActiveSheet is returned. Using the
optional parameters
' the last row in a specific column and or sheet is
returned.
' Use Like : MyVar = LR or MyVar = LR("A") or MyVar =
LR("A","Sheet1")
'---------------------------------------------------------------------------------------
Function LR(Optional MyCol As String, Optional MySh As String) As Long

If MySh = "" Then MySh = ActiveSheet.Name
With Sheets(MySh)
If MyCol = "" Then
LR = .Cells.Find( _
What:="*", _
After:=Range("A1"), _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious).row
Else
LR = .Cells(Rows.Count, MyCol).End(xlUp).row
End If
End With

End Function
'---------------------------------------------------------------------------------------
' Procedure : LC
' DateTime : 07/11/07 11:43
' Author : Michael Beckinsale
' Purpose : To get the last column number. Used without the optional
parameters the last
' column anywhere on the ActiveSheet is returned. Using
the optional parameters
' the last column in a specific row and or sheet is
returned.
' Use Like : MyVar = LC or MyVar = LC(1) or MyVar = LC(1,"Sheet1")
'---------------------------------------------------------------------------------------
Function LC(Optional MyRow As Long, Optional MySh As String) As Long

If MySh = "" Then MySh = ActiveSheet.Name
With Sheets(MySh)
If MyRow = 0 Then
LC = .Cells.Find( _
What:="*", _
After:=Range("A1"), _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious).Column
Else
LC = .Cells(MyRow, Columns.Count).End(xlToLeft).Column
End If
End With

End Function

Regards

Michael