This is a topic that (to my knowledge) is not touched on from CPearson's resource site (which is a very good resource for those looking to use a macro in Excel to programmatically insert code into a new worksheet/workbook, or existing worksheet/workbook).
I currently have Sheet1 containing a series of four to five separate sub macros that perform functions that do not currently affect each other. I have written a macro that, when activated by a user button press, creates a new worksheet and inserts a few lines of code onto that sheet (this task is already complete and functioning properly).
My goal is to add a line to this procedure so that, once the above action has been performed, "line of code xyz #1" will be added to a specific procedure in this CodeModule, namely: "Sub MacroMain()".
I am currently able to (nearly) achieve this result using the following, where "AddCode" is a text String:
ActiveWorkbook.VBProject.VBComponents(Sheets("Sheet1").CodeName).CodeModule.AddFromString AddCode
The issue with this solution however is that it only adds the new line of code to the uppermost portion of Sheet1's CodeModule, and not the specific sub macro Sub MacroMain. Attempts to reference SubMacroMain in the above line unfortunately produce an "Expected Function or variable" error. I assume this is user formatting error on my part.
I was wondering if perhaps there is a certain syntax I should be following with the line of code listed above to make it so the code being inserted in this procedure is placed into a specific macro in Sheet1's CodeModule.
Thank you.