Search code examples
excelexcel-formulaworksheet-functionexcel-udfvba

Using UDF Returned Value in Worksheet Function


I have created a UDF that returns the last populated row in a specific column. Right now, it returns the row number of the last populated row. My question is, how would I go about using this value in a worksheet function?

For example, =COUNTBLANK("B2:B&LastRow(2)), where LastRow(2) returns an integer.


Solution

  • First the UDF

    Public Function LastRow(Rin As Range) As Long
        cl = Rin.Column
        LastRow = Cells(Rows.Count, cl).End(xlUp).Row
    End Function
    

    and then to use it in the worksheet:

    =COUNTBLANK(INDIRECT("B2:B"& LastRow(B:B)))