I dynamically create a userform named UserForm1. In it, I generate textboxes, which will be filled manually by a user. Afterwards I would like to read their value but I don't know how to call the (value of the) textbox.
The following bit of code is used to create and name the textboxes:
With UserForm1 'scaling userform
.Height = max_width
.Width = 600
End With
For test1 = 1 To nr_of_zeros + 1 'create textboxes
Set ctextbox = Controls.Add("forms.textbox.1", test1) 'creating textbox
With ctextbox 'scaling textbox
.Height = 20
.Width = 40
.Top = 40 + 25 * test1
.Left = 400
End With
So the textbox will have the name of the number (integer or long?) of test1.
I tried the following sentences to try to read the value of the textbox into: absorb_text but unsuccesfull so far. Does anybody know the correct complete way to call the above created textbox?
'ctextbox.name = Controls.Add("forms.textbox.1", test1) 'creating textbox
'absorb_text = forms("textbox").Controls(test1).Value
'absorb_text = forms.("UserForm1").textbox.(test1).value
forms.textbox.1.(test1)
strname = TextBox1(test1).Text
(Analog to, one does not call cell "A2" by)
.range("A2")
but with
Thisworkbook.worksheets("sheetname").range("A2").text
Thisworkbook.worksheets("sheetname").range("A2").value
Thisworkbook.worksheets("sheetname").cells(2,1).text
Thisworkbook.worksheets("sheetname").cells(2,1).value
Thank you very much! I was still wondering why/what the 1 does in "forms.textbox.1" I copied it because it worked, but am confused by its function.
Also in light of your discussion below: I believe, technically the code does not look for a control name that is equal to the/a number 1 but to a string character which happens to equal the character 1. hence it is not equal to a number but a character.
*argument against that is that it still works with: `If ctrl.Name = 1 Then' in which case I would think the 1 is treated as a number.