Referring to textbox in userform (VBA)

Tim_Excel_

Well-known Member
Joined
Jul 12, 2016
Messages
512
Hi Forum,

I've written a piece of code that looks in a column and places all unique values in seperate textboxes in a userform.
The next step would be referring to these textboxes in the userform code. Whatever I try however (Userform4.Textbox("box1").Value, or Userform.Textbox1.Value), nothing actually returns the values of the textboxes in the userform. How do I refer to the textboxes?
Code:
Public Sub ytj()

Dim NewBox As MSForms.TextBox


With Workbooks("0700.0006.000.21_01_01").Worksheets(3)

k = 0
l = 0

For i = 2 To .Cells(.Rows.Count, "A").End(xlUp).Row
If Left(Cells(i, 1).Value, 4) <> "" And Left(Cells(i, 1).Value, 4) <> Left(Cells(i - 1, 1).Value, 4) Then
Set NewBox = UserForm4.Controls.Add("Forms.textbox.1")
With NewBox
If i > 2 Then 'the if statement makes sure the first box is placed 30 from the top, and the next boxes 30 + 25 * k
    .Name = "box" & k & ""
    .Top = 30 + 25 * k
    .Left = 10
    .Locked = True
    k = k + 1
Else
    .Name = "box" & k & ""
    .Top = 30
    .Left = 10
    .Locked = True
End If
NewBox.Value = Left(Cells(i, 1).Value, 4)
End With
Set NewBox = UserForm4.Controls.Add("Forms.textbox.1") 'another set of boxes on the right of the other boxes
With NewBox
If i > 2 Then
    .Name = "boxinvul" & k & ""
    .Top = 30 + 25 * l
    .Left = 160
    l = l + 1
Else
    .Name = "boxinvul" & k & ""
    .Top = 30
    .Left = 160
End If
End With
End If
Next i
 
UserForm4.Show vbModeless
 
 
 
End With
End Sub
 
Gallen,

to address your questions about me using k= 0 and that. The if statement is there to assure that the very first box is placed all the way on top (so it's just +30 and not +30 + 25 * k). I did make a few errors when writing the code because I had been staring at it for too long. I need more coffee. It does work now however, so I thank you for your help
 
Upvote 0

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".

Forum statistics

Threads
1,223,911
Messages
6,175,334
Members
452,636
Latest member
laura12345

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