1. A basic function is meant to return a single value to a single cell in the worksheet (the cell containing the function).
2. The function must have a line that assigns the calculated value to the cell containing the function. In the function CubeRt()
Public Function CubeRt(ByVal Radicand as double) As Double
CubeRt = Radicand ^ (1/3) <<<< This line returns the value to the cell
End Function
3. In your function, you have the line: MonthFromDt = Month(DateSerial(yr, 1, 1) + 7 * (wk - 1)) which should return the calculated month to the cell.
The function should have ended with that line; instead, it is followed by the line:
Range("AO" & CStr(k)) = MonthFromDt
You are trying to write the month to a series of cells (2060 cells!) in the worksheet using a loop.
What it is actually doing is trying to call the function from within the function itself (it's called recursion).
The function requires a string to be passed to the variable "s" but you are passing nothing so the program crashes.
The function should look like:
Public Function MonthFromDt(ByVal s As String) As Long
Dim yr As Integer, wk As Integer, d As Date, k As Integer
If s <> "TBC" Then
ary = Split(s, "w")
yr = CInt(ary(0)) + 2000
wk = ary(1)
MonthFromDt = Month(DateSerial(yr, 1, 1) + 7 * (wk - 1))
End If
End Function
I don't know why you need the vb function, it can be done directly in the cell using:
=DATE(LEFT(A1,2)+2000,1,1)+7*(RIGHT(A1,2)-1)
Bookmarks