PDA

View Full Version : Using VBA To copy VBA modules to new wb



klandreth
12-19-2012, 02:58 AM
After "borrowing" snippets of code from various sites on the Internet, I have been able to successfully construct a routine to create a new wb from the current (running) workbook. It copies all the sheets (and their associated code), and here's the important part -- all the modules & as well as the needed references.

My one sticking point is this:

I have all my global variables in a single module. That's all that's in that module.

One of the worksheets has an activate event that uses some of these global variables. As I write this post, I realize maybe I'm doing this backwards. Ead sheet in the wb is copied, then the vba code, then the references. As the sheet in question gets copied, the activate event is triggered in the new wb(I assume by the copy?), but the function fails because I have an 'explicit' statement, and the global variable can't be found because the module containing them hasn't been copied yet.

:banghead: Maybe I need to re-arrange my code? Or am I doing something else wrong?

Thank you for your help, and your awesome website.

Karen

snb
12-19-2012, 03:17 AM
Have a look over here:

http://www.snb-vba.eu/VBA_Excel_VBproject_en.html#L46 (http://www.snb-vba.eu/VBA_Excel_VBproject_en.html#L46)

Bob Phillips
12-19-2012, 06:26 AM
Why not just copy the whole workbook, the code goes with it then.