Using user-defined functions on the worksheet

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,926
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I have a function as follows:

Code:
Public Function abc (a, b, c)

    abc= a + b + c

End Function

I can use it in Excel by typing:

Code:
=abc(1, 2, 3)

which returns the value of 6, as expected.

I have another function which finds the last row on a worksheet:

Code:
Public Function LRow(ByRef wks As Worksheet) As Long
    On Error GoTo Correction
        With wks
        
            LRow = .Cells.Find(What:="*", _
                               After:=.Cells(Rows.Count, Columns.Count), _
                               SearchDirection:=xlPrevious, _
                               SearchOrder:=xlByRows).Row
            
        End With
    
        GoTo Exitpoint
    
Correction:
        LRow = 1
Exitpoint:
    On Error GoTo 0
End Function

I tried typing:

Code:
= LRow(Sheet1)

but it returned #VALUE !

What is wrong?

Thanks
 
Last edited:

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
you cant pass a worksheet object from a worksheet formula but you can pass a Range, so this should work:

Code:
Public Function LRow(ByRef R As Excel.Range) As Long
On Error GoTo Correction


With R.Worksheet
    LRow = .Cells.Find(What:="*", _
                       After:=.Cells(Rows.Count, Columns.Count), _
                       SearchDirection:=xlPrevious, _
                       SearchOrder:=xlByRows).Row
End With
    
Exit Function
    
Correction:
        LRow = 1
End Function


Code:
=LRow(G10)
 
Last edited:
Upvote 0
you cant pass a worksheet object from a worksheet formula but you can pass a Range, so this should work:

Code:
Public Function LRow(ByRef R As Excel.Range) As Long
On Error GoTo Correction


With R.Worksheet
    LRow = .Cells.Find(What:="*", _
                       After:=.Cells(Rows.Count, Columns.Count), _
                       SearchDirection:=xlPrevious, _
                       SearchOrder:=xlByRows).Row
End With
    
Exit Function
    
Correction:
        LRow = 1
End Function


Code:
=LRow(G10)


Thanks, indeed it did work.
 
Upvote 0
you cant pass a worksheet object from a worksheet formula but you can pass a Range, so this should work:

Code:
Public Function LRow(ByRef R As Excel.Range) As Long[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]On Error GoTo Correction[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]With R.Worksheet[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]    LRow = .Cells.Find(What:="*", _[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]                       After:=.Cells(Rows.Count, Columns.Count), _[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]                       SearchDirection:=xlPrevious, _[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]                       SearchOrder:=xlByRows).Row[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]End With[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]    [/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]Exit Function[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]    [/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]Correction:[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]        LRow = 1[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]End Function[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]


Code:
=LRow(G10)



So instead of writing:

Rich (BB code):
= Sum (G1:G100")

can I write:

Rich (BB code):
= Sum (G1:G) & LRow(G10)

expecting to sum from G1 to the last cell in column G?
 
Last edited:
Upvote 0
No. You would have to use either INDIRECT or INDEX, like this:

=Sum(G1:INDEX(G:G,LRow(G10)))

or:

= Sum(INDIRECT("G1:G" & LRow(G10)))
 
Upvote 0
Here is another LRow function for you to consider.... pass the sheet name into it as a quoted text string or a cell reference containing the cell name. Note that it has an optional Boolean argument that defaults to False... True returns last row even if it contains formulas display the empty text string ("") whereas False returns the last row displaying data even if there are formulas displaying the empty text string after it. For cells containing no formulas, it makes no difference what the optional argument is set to.
Code:
[table="width: 500"]
[tr]
	[td]Function LRow(SheetName As String, Optional IncludeFormulaBlanks As Boolean) As Variant
  Dim SearchType As Long
  On Error GoTo EmptySheet
  SearchType = IIf(IncludeFormulaBlanks, xlFormulas, xlValues)
  LRow = Sheets(SheetName).Cells.Find("*", , SearchType, , xlRows, xlPrevious, , , False).Row
  Exit Function
EmptySheet:
  LRow = 1
End Function[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0
No. You would have to use either INDIRECT or INDEX, like this:
=Sum(G1:INDEX(G:G,LRow(G10)))
or:
= Sum(INDIRECT("G1:G" & LRow(G10)))


Here is another LRow function for you to consider.... pass the sheet name into it as a quoted text string or a cell reference containing the cell name. Note that it has an optional Boolean argument that defaults to False... True returns last row even if it contains formulas display the empty text string ("") whereas False returns the last row displaying data even if there are formulas displaying the empty text string after it.
Code:
[/FONT][/COLOR][/LEFT][TABLE="width: 0"]
<tbody style="border-collapse: collapse; width: auto;">[TR]
[TD]Function LRow(SheetName As String, Optional IncludeFormulaBlanks As Boolean) As Variant
  Dim SearchType As Long
  On Error GoTo EmptySheet
  SearchType = IIf(IncludeFormulaBlanks, xlFormulas, xlValues)
  LRow = Sheets(SheetName).Cells.Find("*", , SearchType, , xlRows, xlPrevious, , , False).Row
  Exit Function
EmptySheet:
  LRow = 1
End Function[/TD]
[/TR]
</tbody>[/TABLE]
[LEFT][COLOR=#222222][FONT=Verdana]


Thanks for both tips.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top