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

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
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.
 
Upvote 0
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
 
Upvote 0
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.:)
 
Upvote 0

Forum statistics

Threads
1,213,561
Messages
6,114,312
Members
448,564
Latest member
ED38

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
Back
Top