Value of Numerous TextBoxes using Loop

AmundalA

New Member
Joined
May 10, 2012
Messages
2
Hi!

This is my first post, so I hope someone can help a newbie like me:)

I have a simple userform with over 50 textboxes, named TextBox1, TextBox2, TextBox3 etc.....50.

I want the values (as string) of these textboxes back into my excel sheet.
Is there a way of using loops instead of manually defining every textbox?

Instead of:
'ActiveCell.Offset(0, 1).Select
'ActiveCell = UserForm1.TextBox1
'ActiveCell.Offset(0, 1).Select
'ActiveCell = UserForm1.TextBox2
'ActiveCell.Offset(0, 1).Select
'ActiveCell = UserForm1.TextBox3
and so on and so on....



This is how far I've gotten:
Dim ctrl As Object
Dim NextTextBox As Object
Dim Counter As Integer
Dim TextBox As Object

Counter = 0
TextBoxLoop:

Counter = Counter + 1

If Counter < 6 Then
NextTextBox = "UserForm1.TextBox" & Counter
ActiveCell.Offset(0, 1).Select
ActiveCell = "UserForm1.TextBox" & Counter ' Returns only the name of the textbox I want the values from. How to I get the values in the worksheet in Excel?

GoTo TextBoxLoop
End If




But this it only returns the Textbox-name in the cell instead for the actual content of the TextBox.
I know this is probably way of, but when I have managed to get a variable to be called the same as the name of the textbox I want values from...

Please don't let me manually go through 50 textboxes:)
Will be happy for any reply!
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Try something like this...

Code:
    [color=darkblue]Dim[/color] i [color=darkblue]As[/color] [color=darkblue]Integer[/color]
    
    [color=darkblue]For[/color] i = 1 [color=darkblue]To[/color] 50
        ActiveCell.Offset(i - 1).Value = Me.Controls("TextBox" & i).Value
    [color=darkblue]Next[/color] i
 
Upvote 0
thaaaank you! I was going crazy, and you make it so easy:)

Made just a small ajustment, because I wanted the result horisontaly displayed. I just put in the Rowoffset 0.

Dim i As Integer

For i = 1 To 50
ActiveCell.Offset(0, i - 1).Value = Me.Controls("TextBox" & i).Value
Next i


Thanks again for your help!!
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

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