@eng_diego
The function is straightforward and does not seem to be the problem.

Maybe you can add some codes to avoid the error.