Your posts are confusing.
You talk about external 9volt and USB, and then you talk about 5volt and USB.
And you didn't mention where you have connected external supply to.
A Mega needs an absolute minimum of 6.6volt on the V-in pin, or 7.3volt on the DC socket (including dips).
5volt external can be connected to the 5volt pin, but that could give unexpected results in combination with USB supply.
A sketch is saved to flash during upload, and does NOT change until you upload a new sketch.
A bad supply could reset/corrupt the Mega during bootup, but that does not affect the stored sketch.
Not sure what else you have attached to the Mega's 5volt pin.
I hope not the 152 addressable LEDs you mentioned in another thread.
Post a connection diagram, and exact specs of the supply.
Leo..