Basically I would think this should be pretty simple, but everything that I have tried is not working for me. First, I am using Excel 2016 and working with a workbook where when needed it copies one worksheet called template to another. The template worksheet has a simple Print button on it. The copy procedure copies the print button OK, but the simple CommandButton1_Click code which simple contains "PrintSelection" does not get copied over. I want to automate this so that other users can add new sheets and the Print button will be ready for their use.
I have researched and tried a number of things. I have added the reference to MS VB for Applications Extensibility 5.3 to the VB project window and I have tried a couple of different subs but they fail with the message "subscript out or range" errors.
One example is"
"Sub copySheetCode()
Dim src As CodeModule
Dim dest As CodeModule
Set src = ThisWorkbook.VBProject.VBComponents("Template").CodeModule
Set dest = ThisWorkbook("test").VBProject.VBComponents _
.CodeModule
dest.DeleteLines 1, dest.CountOfLines
dest.AddFromString src.Lines(1, src.CountOfLines)
End Sub
"
The error occurs on the first like starting with "Set src"
Does anyone have any idea what I might need to do to resolve this, or any other way of achieving what I want to do?
Thanks in advance, all help is greatly appreciated.
Bookmarks