Dynamically Create and Fill Value of Userform Textbox in VBA

bemp87

Board Regular
Joined
Dec 10, 2016
Messages
102
Hi Community, I am stuck in a VBA conundrum. I am trying to create multiple UserForm Textboxes in vba and Fill each one with a unique value either by index, or by some other means. I am able to get them to fill but when I do it seems that all the text boxes are being filled with the same values due to the there is no unique name distinction from textbox1 to textbox2, etc. I would imagine there has to be a way to do this, and I am stuck. Any help would be appreciated.

VBA Code:
Sub CharLoad()

    Dim i As Integer
    Dim j As Integer
    Dim k As Integer
    Dim dict As New Scripting.Dictionary
    Dim charArray(1 To 4) As String
    Dim ColHeader As String
    Dim txtBox As MSForms.TextBox
    Dim h As Integer
   
    h = 0
    j = WorksheetFunction.CountA(Sheets(1).Range("A:A"))
   
    For i = 1 To j
        charArray(1) = Worksheets(1).Cells(i, 2)
        charArray(2) = Worksheets(1).Cells(i, 3)
        charArray(3) = Worksheets(1).Cells(i, 4)
        charArray(4) = Worksheets(1).Cells(i, 5)
        dict.Add Worksheets(1).Cells(i, 1), charArray
    Next i
    b = dict.Keys
    For k = 1 To j
        Set txtBox = UserForm1.mainFrm.Controls.Add("Forms.TextBox.1", "txtBox")
        'tb = UserForm1.mainFrm.Controls.Add("Forms.TextBox.1", "txtBox")
        txtBox.Top = h + 20
        txtBox.Left = 6
        h = h + 20
        'txtBox.BackColor = RGB(255, 255, 0)
            For i = 1 To j
            [COLOR=rgb(247, 218, 100)]UserForm1.mainFrm.txtBox.Value = i & ".  " & dict.Keys(i - 1)[/COLOR]
            'UserForm1.ListBox2.AddItem dict.Item(b(i - 1))(1)
            UserForm1.ListBox3.AddItem dict.Item(b(i - 1))(2)
            UserForm1.ListBox4.AddItem dict.Item(b(i - 1))(3)
            UserForm1.ListBox5.AddItem dict.Item(b(i - 1))(4)
            'Debug.Print dict.Item(b(i - 1))(k)
        Next i
    Next k
End Sub

The highlighted line of code in yellow is what keeps getting filled with the same value which is essentially creating 26 text boxes all having the same value which is the last value being retrived from the dictionary.
 
Glad we could help & thanks for the feedback.
 
Upvote 0

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)

Forum statistics

Threads
1,214,923
Messages
6,122,283
Members
449,075
Latest member
staticfluids

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