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

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
step through the code with f8

you set autosize, then go onto changing the size, maybe auto is taking priority
 
Upvote 0
You were right, autosize was taking priority. Thank you so much! I've seriously been going crazy over this. Have a good one
 
Upvote 0

Forum statistics

Threads
1,215,054
Messages
6,122,897
Members
449,097
Latest member
dbomb1414

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