I'm generating a module programmatically when a command button is clicked on a modeless form. At the end of the code the form closes automatically without me explicitly unloading or hiding it. The form will stay open if I make it modal, but then the next modeless form that I open will do the same thing. I'm guessing this is happening to bring focus onto the new code in the VBA editor window at the first opportunity, but I want my program to keep running instead! Any ideas on how to get the form to stay open?
I'm using Excel 2010 Pro, VBA 7.0
Private Sub cmdGenerateModule_Click()
Dim mdlUserDefined As Object
Set mdlUserDefined = ActiveWorkbook.VBProject.VBComponents.Add(1)
mdlUserDefined.CodeModule.AddFromString "Public Sub Test_Code()" & Chr(13) & "End Sub"
UserForm1.Show ' this doesn't help
Debug.Print "debug test" ' this line prints in the debugger, so the code executes before closing the form
End Sub