Attempting to write a function or cell formula that will update a target cell, B132, if the current cell, B133, is larger.
Cell B133 contains #Value! after function reaches the .Value = nNew assignment. If B132 is greater than MAX(B6:B130) then all is well and B133 displays the value for MAX(B6:B130), the smaller value as it should. In debug mode all variables show the same type.
The range B6:B130 is a range of share prices with B132 being the high water mark overtime to hold, not just the max( B6:B130).
B132 is the high water mark spanning months, B6: B130 of time
B133 is the cell to contain the formula
spreadsheet sample values
B6 10.347
.
.
.
.
B130 115.027
B132 4
B133 =+SetHighWaterMark( CELL("address", B$132), CELL("contents", B$132), MAX( B$6:B$130 ))
=======================
‘ Test High Water Mark function
Sub hwm()
Dim nX
‘ This works and returns the correct value and
' sets the target cell
‘ and returns the correct value. 4 in this case.
nX = SetHighWaterMark("$B$132", 1, 4)
End Sub
‘’--- EOS hwm()
Function SetHighWaterMark(Target, nLast, nNew) As Double
Dim nCurVal
If nLast < nNew Then
Stop
' This returns the correct value contained in
' Target cell (as expected)
nCurVal = Range(Target).Value
‘ this fails if called from within a cell
' no error message detectable
Range(Target).Value = nNew
End If
SetHighWaterMark = nNew
End Function
‘’--- EOF SetHighWaterMark()
==============================
Thanks,
Daryl