I have a custom function which I've written in VBA.
At the start of the code, the function checks that the input data is in the right format and displays an error message if not.
The problem I have is that if I break the input data (e.g. put some text in a cell which the function requires to be numeric), then every cell which contains my function and references that data will display an error message. So I get loads of error messages all at once.
I'd like to be able to change my function so it only displays the error message if the user is actively editing cell (similar to usual excel function error messages).
I thought I'd be able to use something like Worksheet_Change but I don't see how I could embed that in my function.
Another option would be to only allow 1 error message at a time but no idea how to set that up either.
Any ideas would be appreciated!
Here's the start of my code with the first input data check and error message:
Public Function OnLevelFactor(effective_dates As Range, rate_changes As Range, OnLevelYear As
Range) As Double
'do they only contain numbers (/dates)?
If (WorksheetFunction.Count(effective_dates) + WorksheetFunction.CountBlank(effective_dates)) <> effective_dates.Count() Then
Msg = "Dates " & effective_dates.Address & " must be numeric."
MsgBox Msg, , "Error: OnLevelFactor", Err.HelpFile, Err.HelpContext
Exit Function
End If
'rest of code
End Function
EDIT
I found this code which works pretty well:
Public Function OnLevelFactor(effective_dates As Range, rate_changes As Range, OnLevelYear As
Range) As Double
'Check if cell is being actively edited before triggering error-handling
Dim caller As Range
Set caller = Application.caller
MsgBoxTrigger = Not Intersect(caller, Application.ActiveCell) Is Nothing
And then each error check begins with "If MsgBoxTrigger Then". Any thoughts on this approach would be appreciated.