VBA – Copy Worksheet

Copying worksheet in VBA is actually a trivial task – use the macro recorder see the code and use it further. Anyway, if you are coding professionally with VBA (yup, we still exist:), you somehow get stomach problems, whenever you see .Activate and .Select in a production code. I even consider this as one of the top VBA errors.

Thus, the recording would not work. Then you can probably Google and you would see that mainly the first answers use those two. That’s bad, if you are thinking to get paid writing VBA.

Long story short, in order to save your stomach problems, this is the code I consider ok:

Quite a bit of code, but still not bad. Furthermore, there is no check whether the worksheet from which we copy is present and this is on purpose. Usually I get this one from a collection of worksheet or something, thus the error would be handled correspondingly in the mother function.

If you want to copy 3-4 worksheets, this is a possibility:

I have even decided to add this one to my GitHub Boilerplate – if you want to change something, make a pull request! 🙂

Tagged with: , , , , ,