First, we can know that Maximum limit of Unique cell formats/cell styles is 65490 from this document.
So you need to reduce the use of cell formatting in the code.
And I suggest to try method three and method four mentioned in this document.
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.