delcampo95
New Member
- Joined
- May 7, 2021
- Messages
- 4
- Office Version
- 365
- Platform
- Windows
Hi all - I'm at my wits end. I am a forum- and Google-taught VBA coder, and I am trying to figure out some weird behavior with IsError being interpreted strangely in VBA code.
I made a function to return the numerical type for a Data Validation for an input cell (ex: 3 for a List, 7 for Custom), and to return a -1 when there is no Data Validation applied to the cell. Here is my function (I know it is not very streamlined - I have broken it up into big steps to try to troubleshoot):
Instead of returning a -1 upon having no Data Validation, the function returns an error of #VALUE!. It also appears that this function is aborting and returning this value when it gets to the following line:
Am I using IsError in the wrong way?
I made a function to return the numerical type for a Data Validation for an input cell (ex: 3 for a List, 7 for Custom), and to return a -1 when there is no Data Validation applied to the cell. Here is my function (I know it is not very streamlined - I have broken it up into big steps to try to troubleshoot):
VBA Code:
Function DataValidationType(InputCell As Range) As Variant
'Returns a data validation type from a cell
Dim ErrorFlag As Variant
'Sets function to refresh automatically
Application.Volatile True
'If multiple cells are passed to function, fail with error
If (InputCell.Cells.Count > 1) Then
DataValidationType = CVErr(xlErrValue)
Exit Function
End If
'Set flag if there is no data validation set for this cell
ErrorFlag = IsError(InputCell.Validation.Type)
'If there is no data validation set for this cell, return -1
If ErrorFlag Then
DataValidationType = -1
Exit Function
End If
'If no errors encountered, return the validation type (an index number)
DataValidationType = InputCell.Validation.Type
End Function
Instead of returning a -1 upon having no Data Validation, the function returns an error of #VALUE!. It also appears that this function is aborting and returning this value when it gets to the following line:
VBA Code:
ErrorFlag = IsError(InputCell.Validation.Type)
Am I using IsError in the wrong way?