Use VBA to Add textbox to existing userform

Jonnyoforem

New Member
Joined
May 22, 2015
Messages
32
I'm trying to create a macro that will increase the height of a pre-existing user form, then add a textbox. I want this done at design-time so it will be permanent after running. I can't do it graphically because other users will be deciding when these text boxes need to be added. Here's the code I have so far.

Code:
Sub Practice()
    Dim hght As Single
    Dim NameUserForm As String
    Dim MyUserForm As Object
    Dim NewTextBox As MSForms.TextBox
    
    'Name of userform
    NameUserForm = "test"
    
    Set MyUserForm = ThisWorkbook.VBProject _
    .VBComponents(NameUserForm)
    
    hght = MyUserForm.Properties("Height")
    
    With MyUserForm
    
        .Properties("Height") = hght + 25
        
    End With
        
    Set NewTextBox = MyUserForm.Controls.Add("Forms.TextBox.1")
    
    With NewTextBox
        .TextAlign = fmTextAlignCenter
        .Width = 66
        .Height = 18
        .Left = 40
        .Top = hght
    End With
            
    test.Show
    
End Sub

The existing user form is called test and when I debug my code I get an error on the following line

Code:
Set NewTextBox = MyUserForm.Designer.Controls.Add("Forms.TextBox.1")

The error is the following: Run-time error '-2147319767 (80028029)': Invalid forward reference, or reference to uncompiled type.

Thanks in advance for any help. And if I could also get an explanation as to why my code is failing that'd be great. I'd really like to tame the VBA beast.
 
I don't see why you would have either Load or Unload in the code.:eek:
 
Upvote 0

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.

Forum statistics

Threads
1,215,126
Messages
6,123,200
Members
449,090
Latest member
bes000

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