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
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
MsgBox UserForm4.TextBox1.Text or similar produces a "Method or data member not found" error, indicating the textbox doesn't exist.

I renamed the textboxes in the code with the .Name property, maybe that helps?
 
Last edited:
Upvote 0
That would do it! The name of the textbox should appear in intellisense when typing your code... If it doesn't then there's your problem. Unless you are creating textboxes dynamically I can't think of a reason to change a textbox's name in code. To be honest I didn't think you could.
 
Upvote 0
I have disabled the textbox renaming pieces of code without effect.
Intellisense doesn't even give TextBox as an option when typing 'Userform4.'
 
Upvote 0
OK I just read your code a little more thoroughly. You are dynamically creating textboxes and renaming them. Have you stepped through line by line ensuring the names are generated as you expect? I will try and recreate your scenario myself too
 
Upvote 0
I tried doing that, but I can't refer to the textboxes, making it virtually impossible to check. The boxes are created however, which I can see when the userform opens. However, the textboxes are temporary (only last when the code still runs) so upon closing the code I can't check the properties from the userform either to check the boxes' names. It's a difficult one really
 
Upvote 0
Code:
MsgBox Me.Controls("box1").Value

That's how easy it was.

Why didn't I find this earlier?



eitherway, thanks for the help.
 
Upvote 0
Having read through and done some testing you've really got yourself in a tizz :)

As you name the Textboxes dynamically, their names can be different depending on the values in the variables:

Code:
[COLOR=#333333] .Name = "box" & k & ""[/COLOR]
(btw I can't see a valid reason for the & "" part)

I'm guessing this is so you can loop through them later?

Also k is set to 0 so the first box's name would be 'box' as k is empty
Code:
[COLOR=#574123].Name = "box" & k & ""[/COLOR]
so you'd need :
Code:
[COLOR=#574123].Name = "box" & Cstr(k)[/COLOR]
This would give the box the name 'box0'

I got confused with what you were trying to achieve with 'k' as it only gets increased in certain situations. Make sure you understand :)

So finally I'm guessing you have more code that references these textboxes? Can you show that too?


Below is some rewritten code with comments/questions see if it makes sense or if I've got confused

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
                    'name is set whichever outcome so just one line needed:
                    .Name = "box" & CStr(k)
                    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
                        .Top = 30 + 25 * k
                        .Left = 10
                        .Locked = True
                        k = k + 1
                    Else
                        .Top = 30
                        .Left = 10
                        .Locked = True
                        'why is k not increased here?
                    End If
                    .Value = Left(Cells(i, 1).Value, 4)
                End With
                Set NewBox2 = UserForm4.Controls.Add("Forms.textbox.1") 'another set of boxes on the right of the other boxes
                With NewBox2
                    '???Shouldn't this be cstr(l) not k???
                    .Name = "boxinvul" & CStr(k)
                    If i > 2 Then
                        .Top = 30 + 25 * l
                        .Left = 160
                        l = l + 1
                    Else
                        .Top = 30
                        .Left = 160
                        'why is l not increased here?
                    End If
                End With
            End If
        Next i
        
        UserForm4.Show vbModeless
         
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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