Inserting code in a userform at runtime, while the form is created at design time

craig.penny

Well-known Member
Joined
May 8, 2009
Messages
656
Hi all,

I managed to get everything working for a run-time created userform but because it's giving me debugging headaches I want to make the form itself at design time but still have everything else added at runtime. Everything was easy to change except inserting code into the form's code module.


HTML:
With MDSByYear.CodeModule
    Line = .CountOfLines
 
    .InsertLines Line + 1, "Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)"
    .InsertLines Line + 2, "Cancel = True"
    .InsertLines Line + 4, "End Sub"
 
    .InsertLines Line + 5, "Private Sub DoneButton_Click()"
    For loopCount = 1 To mdsSize
 
      For YrLoopCount = minYear To maxYear + 1
 
        .InsertLines Line + 10 * (maxYear - minYear + 5) + YrLoopCount - minYear + 1, _
        "BootBoolean(" & loopCount & "," & YrLoopCount & "," & SelCaseCount & ") = me.checkbox" & (loopCount - 1) * (maxYear - minYear + 1 + 1) + (YrLoopCount - minYear + 1) & ".Value"
 
      Next YrLoopCount
 
    Next loopCount
 
    Line = .CountOfLines
    .InsertLines Line + 5, "Me.Hide"
    .InsertLines Line + 10, "End Sub"
 
  End With

The first line of this code is where it stops. Is there a way to insert code at run-time into a form that was created at design-time?

Thanks for any help!
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

craig.penny

Well-known Member
Joined
May 8, 2009
Messages
656
Andrew,

Thanks alot for answering. The example at that link is when the form itself is created at run-time. Mine is created at design-time, it's only the code in the form's module I want to do at run-time. Any ideas?

Thanks again.
 

craig.penny

Well-known Member
Joined
May 8, 2009
Messages
656

ADVERTISEMENT

Code:
Set MDSByYear = MDSByYearForm
 
'where MDSByYearForm is the name of the userform
 
'I was able to add all kinds of controls and frames to it but it errored on
 
With MDSByYear.CodeModule
 
'with error 438,  Object doesn't support this property or method
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,282
Office Version
  1. 365
Platform
  1. Windows
Craig

Why are you doing this?

I know it's possible but I really don't see why you would want to create a userform and/or code for it like this.

I'm pretty sure that whatever you are trying to achieve can be done using another approach.

And that approach would probably give you less headaches.:)
 

Watch MrExcel Video

Forum statistics

Threads
1,129,754
Messages
5,638,170
Members
417,011
Latest member
Amaden95

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top