In the VB editor, insert a new module, then ...
Option Explicit
Function LastNPostion(byVal variable1, byVal variable2, byVal variable3, ... )
...
... code goes here to ...
... calculate LnP_result
...
LastNPostion = LnP_result
End Function
"Option Explicit" & "byVal" are optional.