We have attached a corrected UDF code and some screen shot of the input and output.













We hope it will help you resolve the issue.

If you have any issue feel free to revert.