This forum is for VBA codes and the codes wont work in excel online.
You may have to check the possibility of Office Scripts in Excel on the web.
Expecting experts in this forum to comment and help you in this