JenniferMurphy
Well-known Member
- Joined
- Jul 23, 2011
- Messages
- 2,694
- Office Version
- 365
- Platform
- Windows
I have a complicated UDF that takes several ranges as parameters. Each of these ranges can have up to 30-40 values. Each of these values must conform to certain limits, which vary depending on the values in the corresponding cells in the other ranges.
The UDF does extensive validity checking to ensure that all of the ranges are of the correct shape and the values meet the requirements. If any of these tests fail, I return a Value error. The problem is trying to figure out which cell caused the error.
One solution I came up with is to include a MsgBox explaining what the error is and where it occurred and then force a breakpoint so I can do further live debugging.
But I do not want this code to be active all the time. Many times I can figure out what the error is, so I don't need it. But the big problem is if the error is caused by a bug in my code, then it will occur on every call, which means that I will have clear dozens or hundreds of MsgBox dialogs or comment them out and then remember to comment them back in.
My next solution was to comment all of the MsgBox statements and only un-comment them if there is an error I cannot find.
But this is not ideal, either. I have 8-10 calls to MsgBox, so I'd have to find them all and then re-comment them out after I find the bug. My next solution was to add a switch that is either a constant or a parameter to turn all of the MsgBox statements on in one swell foop.
Once the code is fairly stable, I can comment out these statements to improve efficiency and un-comment them if the need arises later.
Two questions:
Thanks
The UDF does extensive validity checking to ensure that all of the ranges are of the correct shape and the values meet the requirements. If any of these tests fail, I return a Value error. The problem is trying to figure out which cell caused the error.
One solution I came up with is to include a MsgBox explaining what the error is and where it occurred and then force a breakpoint so I can do further live debugging.
Code:
MsgBox "message...", , MyName: Stop
WtdRtg = CVErr(xlErrValue)
Exit Function
But I do not want this code to be active all the time. Many times I can figure out what the error is, so I don't need it. But the big problem is if the error is caused by a bug in my code, then it will occur on every call, which means that I will have clear dozens or hundreds of MsgBox dialogs or comment them out and then remember to comment them back in.
My next solution was to comment all of the MsgBox statements and only un-comment them if there is an error I cannot find.
Code:
' MsgBox "message...", , MyName: Stop
WtdRtg = CVErr(xlErrValue)
Exit Function
But this is not ideal, either. I have 8-10 calls to MsgBox, so I'd have to find them all and then re-comment them out after I find the bug. My next solution was to add a switch that is either a constant or a parameter to turn all of the MsgBox statements on in one swell foop.
Code:
Const MsgSw as Boolean = False 'Turn MsgBox off
Const MsgSw as Boolean = True 'Turn MsgBox on
. . .
If MsgSw then MsgBox "message...", , MyName: Stop
WtdRtg = CVErr(xlErrValue)
Exit Function
Once the code is fairly stable, I can comment out these statements to improve efficiency and un-comment them if the need arises later.
Two questions:
- Is this method inefficient? None of this code will be inside a loop of more than 30-40 iterations
- Is there a better way
Thanks