Userform created using code can't edit textbox

Jonnyoforem

New Member
Joined
May 22, 2015
Messages
32
I have a function that creates a user form with certain specifications that I want. I want the user to specify how many textboxes are created when they run the function (That's what OpArray represents). The only problem is when the textboxes are created, they're supposed to be a certain size but nothing I do sizes them properly. I don't get any errors, I just get textboxes with a width of 12.5 instead of the 66 that I specify. Here's the pertinent part of my code.

Code:
Function GetOption1(OpArray)
     Dim TempForm As Object
     Dim NewTextBox As MSForms.TextBox
     Dim i As Integer, TopPos As Integer

'Hide VBE window to prevent screen flashing
    Application.VBE.MainWindow.Visible = False
    
    'Create the UserForm
    Set TempForm = _
      ThisWorkbook.VBProject.VBComponents.Add(3)
        TempForm.Properties("Width") = 800

'Add the TextBoxes
    TopPos = 4
    'MaxWidth = 0 'Stores width of widest TextBox
    For i = LBound(OpArray) To UBound(OpArray)
        Set NewTextBox = TempForm.Designer.Controls. _
            Add("Forms.TextBox.1")
        With NewTextBox
            .Width = 66
            '.Value = OpArray(i)
            .Height = 100
            .Left = 8
            .Top = TopPos
            .Tag = i
            .AutoSize = True
            If Default = i Then .Value = True
            If .Width > MaxWidth _
            Then MaxWidth = .Width
        End With
        TopPos = TopPos + 20
    Next i

VBA.UserForms.Add(TempForm.Name).Show

End Function

I'm pretty new at manipulating VBA components. Any help would be appreciated. This is seriously just baffling me.
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off

mole999

Well-known Member
Joined
Oct 23, 2004
Messages
10,524
Office Version
  1. 2019
  2. 2016
  3. 2013
Platform
  1. Windows
step through the code with f8

you set autosize, then go onto changing the size, maybe auto is taking priority
 

Jonnyoforem

New Member
Joined
May 22, 2015
Messages
32
You were right, autosize was taking priority. Thank you so much! I've seriously been going crazy over this. Have a good one
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,196
Messages
5,835,925
Members
430,396
Latest member
dzifna

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