We help IT Professionals succeed at work.

VBA - How to create a workbook containing workbook event handlers as well as userforms.

Saqib Husain, Syed
I have a workbook which generates blank monthly timesheets for individual employees. The employee selects the name, designation, month and year and clicks a button. This copies a blank timesheet worksheet (which contains macros in the worksheet module) to a new workbook. The new workbook is ready with the worksheet event handlers for the individual.

I now need to extend the capabilities of the workbook to have workbook event handlers and also userforms. I am not clear on how to use VBA to create such workbooks.

One way I can see is to use a macro to write the macros. But this means that every individual's computer should have this feature enabled. I do not think I can go with this.

Another way could be to copy the entire workbook to a new workbook (I am not sure how I would do that) and then delete the other sheets which are not supposed to be copied over. But then the workbook module would contain all the macros related to generating the new workbook in addition to the ones needed to use the timesheet.

Can someone guide me or suggest any other method to be able to copy the macros to a new workbook?
Watch Question

Top Expert 2008
Creating a copy of the master workbook is a commonly used technique. I've used it a few times.

There are two methods:

Save a copy of the workbook and delete what you don't want.

Create a new workbook and copy what you do want.

Place the code that you want in the copy workbook in separate code modules and only copy those modules. Same with user forms.

I can provide code to copy code modules and user forms.

Top Expert 2008

Use the SaveAsCopy method to create a copy of the current workbook without changing the connection to the originally open workbook. The copy saved is closed.

   ThisWorkbook.SaveCopyAs "C:\Full\Path\To\File.xls"

I think I have got it....Create a separate file which contains all the macros and modules required and let the master file change the data in that "separate" file and save it for the individual.

Thanks for your suggestions which gave me this idea. Will close the question when I have done and tested the application.
This method avoids accessing code through code