Consulting

Results 1 to 4 of 4

Thread: Solved: Using For Loop to get info from Text box

  1. #1
    VBAX Regular
    Joined
    Feb 2008
    Location
    atlanta
    Posts
    20
    Location

    Question Solved: Using For Loop to get info from Text box

    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.

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    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.

    [vba]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[/vba]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    VBAX Regular
    Joined
    Feb 2008
    Location
    atlanta
    Posts
    20
    Location

    thank you

    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!

  4. #4
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    You are welcome. Be sure to mark your thread solved using the thread tools at the top of the page.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •