Results 1 to 3 of 3

Looping through text boxes on a user form

This is a discussion on Looping through text boxes on a user form within the Excel Questions forums, part of the Question Forums category; On a user form, I have 30 text boxes named txtFund1 through txtFund30 . In VBA , I am trying ...

  1. #1
    MrExcel MVP
    Moderator
    Joe4's Avatar
    Join Date
    Aug 2002
    Posts
    27,239

    Default Looping through text boxes on a user form

    On a user form, I have 30 text boxes named txtFund1 through txtFund30. In VBA, I am trying to access these boxes to initialize them, and update values on my Excel spreadsheet with these values.

    I can to do all this pretty easily by writing a line for each inidividual text box. However, I want to be more efficient and use a loop. However, I can not get it to work. Here is my code to initialize the values, that isn't working:

    Code:
        Dim i As Integer
        Dim MyTextBox
        For i = 1 To 30
            MyTextBox = txtFund & i
            MyTextBox.Value = ""
        Next i
    What do I need to do to be able to loop through all 30 text boxes? Keep in mind that there are also other text boxes on my form, so I can't just through all text boxes on the form.
    TIPS FOR FINDING EXCEL SOLUTIONS
    1. Use the built-in Help that comes with Excel/Access
    2. Use the Search functionality on this board
    3. A lot of VBA code can be acquired by using the Macro Recorder.

    "Give a man a fish, feed him for a day. Teach a man to fish, feed him for life!"

  2. #2
    MrExcel MVP
    Moderator
    Andrew Poulsom's Avatar
    Join Date
    Jul 2002
    Posts
    69,330

    Default Re: Looping through text boxes on a user form

    Try:

    Code:
    Dim i As Integer 
    For i = 1 To 30 
        Controls("txtFund" & i).Value = "" 
    Next i

  3. #3
    MrExcel MVP
    Moderator
    Joe4's Avatar
    Join Date
    Aug 2002
    Posts
    27,239

    Default Re: Looping through text boxes on a user form

    Andrew,

    Thank you!!! That was just what I needed.
    TIPS FOR FINDING EXCEL SOLUTIONS
    1. Use the built-in Help that comes with Excel/Access
    2. Use the Search functionality on this board
    3. A lot of VBA code can be acquired by using the Macro Recorder.

    "Give a man a fish, feed him for a day. Teach a man to fish, feed him for life!"

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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


DMCA.com