outputFileName1.lastIndexOf(".xlsx") should be fine in a loop or not. If you have a compiler message and can't see how to fix it, post the whole mesage and the code to which it refers.

---

I...