I have an Excel .xlam file that adds a button in the ribbon to do the following:
You can "export" and "import" .bas files programmatically. To do what you are asking, that would have to be the approach. I don't believe it's possible to modify the code in memory. See this article