The only problem is that while it copies the macro, the button assigned to it keeps the link from the myTemplate workbook.
Yep, just realized that. When testing, I had the template file open. Naturally, the macro “worked”.
There is a very simple way of getting around the problem.
Remove the Forms button from the “Data” worksheet in myTemplate.xlt.
Replace with a Command Button from the Toolbox menu (rename the button to something more appropriate).
In the worksheet module of “Data” (myTemplate.xlt), put:
Code:
Private Sub CommandButton1_Click()
Call myFormat
End Sub
The “myFormat” macro in Data.xls now works when the template file is closed (when the “Data” worksheet is copied, the Command Button and the associated macro, travel with the worksheet).
In fact, you could dump the entire routine of copying Module2 by putting the contents of the “myFormat” macro in the Command Button click event.
Naturally, I don’t know how Norton reacts to the above.
I adapted the following code that I saw on the Net. Apparently it works, but it doesn’t:
Sub CopyOneModule2()
'http://www.cpearson.com/excel/vbe.htm
Dim Fname As String
Dim newwb As Workbook
Dim newws As Worksheet
Set newwb = Workbooks("Data.xls")
Set newws = ActiveSheet
With ThisWorkbook
Fname = .Path & "\code.txt"
.VBProject.VBComponents("Module2").Export Fname
End With
'The next line upsets Norton
'Workbooks("Data.xls").VBProject.VBComponents.Import Fname
Application.Run "imprt", Fname
Kill Fname
newwb.newws.Shapes("Button 1").OnAction = newwb.Name & "!" & "MyFormat"
End Sub
I will keep working on the above macro. One day it may come in handy.
Regards,
Mike