How do I determine whether an optional parameter was passed to a VBA/Excel 97 function ? The function below is supposed to look up a value in the previous sheet (default=same address). This works and will allow me to make a “worksheet chain” with little to no adaptations.
However, the second parameter is causing problems, see code. How to solve this ? Or is there a much simpler work-around than this, kind of “Offset accross worksheets” ?
Function ValPrevSheet(Optional SourceAddr As String, Optional InitVal As Variant) As Variant
Dim SheetNo As Integer, TargetAddr As String
With Application.Caller ‘cell which called function
TargetAddr = .Address
SheetNo = .Parent.Index ‘determine its worksheet _number_
If SourceAddr = “” Then SourceAddr = TargetAddr ‘define default
End With
‘Debugging only
MsgBox “Sheet ” & SheetNo & ” at ” & TargetAddr _
& ” to have value of previous sheet at ” & SourceAddr
If SheetNo = 1 Then ‘No previous sheet
‘See whether the default is given in the callup, if not ask for it
‘If IsEmpty(InitVal) = True Then ‘does not work, why ??
‘If Len(InitVal) = 0 Or Len(InitVal) = Null Then ‘does not work either
If InitVal = “” Then ‘does work but “” has to be passed, arghhhh…
MsgBox “Killroy was here” ‘Debugging only,
ValPrevSheet = InputBox(“Indicate init value for first sheet:”, “ValPrevSheet”)
Else
ValPrevSheet = InitVal
End If
Else
ValPrevSheet = Worksheets.Item(SheetNo – 1).Range(SourceAddr).Value
End If
End Function