I'm afraid I was celebrating too soon. I copypasted the code in a module, and after making the above changes and going through it using F8, it worked. However, when I use the code as a function, and run it from a form by pressing a command button, it again says whoops.