PDA

View Full Version : Solved: Using For Loop to get info from Text box



abhiker
05-21-2008, 07:28 PM
i have multiple text boxes on my userform. i would like to get the values of each using a for loop. the text boxes are named txtBox1, txtBox2,...
i would like to do something like this:

For i = 1 to 10
Print ("txtBox" & i).value ' trying to print value of txtBox1
Next i

can someone point me in the right direction? thanks in advance.

lucas
05-21-2008, 08:25 PM
Here is an example for putting the value of three textboxes into the next row of the sheet....not sure what you mean by print...I think you need to put them into an array as shown.

Private Sub CommandButton1_Click()
Dim i As Integer
Dim xControl As Control
Dim rowNumber As Long: rowNumber = 1
Dim valuesRRay(1 To 40) As String
rowNumber = ThisWorkbook.Sheets("sheet1").Range("a65536").End(xlUp).Row + 1
'set the 3 to the number of textboxes you have
For i = 1 To 3
Set xControl = Me.Controls("TextBox" & CStr(i))
valuesRRay(i) = xControl.Text
Next i
Range(Cells(rowNumber, 1), Cells(rowNumber, 40)).Value = valuesRRay
End Sub

abhiker
05-22-2008, 05:27 AM
thank you for the solution. and your right about the print thing. i was just using that for illustration purposes. i am really using the value of the textboxs to output some xml type code. thanks a ton. it works beautifully!

lucas
05-22-2008, 06:24 AM
You are welcome. Be sure to mark your thread solved using the thread tools at the top of the page.