+ Reply to Thread
Results 1 to 4 of 4

Variable Controls

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-09-2009
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    315

    Variable Controls

    Hi

    What's the syntax for referring to a variable control name?

    I have a userform with 20 textboxes (txtAmount1, txtAmount2 etc) and I want to loop through each of those controls, so I want to do something like:

    For varRow = 1 To 20
        Me.txtAmount" & varRow & ".text = "whatever"
    Next varRow

    I know I can loop through all controls on the form (and restrict based on control name) but I want to be able to do something similar to the above for other reasons: I have another column of textboxes (txtSupplier1, txtSupplier2 etc) and if I update txtSupplier2 then I want to update the corresponding txtAmount2.

    Any help is appreciated.

    Dion
    Last edited by mojo249; 09-10-2011 at 11:33 AM.

  2. #2
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Re: Variable Controls

    Something like..

    For X = 1 To 3
        If UserForm1.Controls(X).Name Like "TextBox*" Then
            UserForm1.Controls(X).Text = "Whatever"
    End If
    Next X
    Martin

  3. #3
    Valued Forum Contributor
    Join Date
    06-19-2010
    Location
    Holywell, N Wales, UK
    MS-Off Ver
    Excel 2013
    Posts
    470

    Re: Variable Controls

    Try
    Private Sub UserForm_Click()
    Dim varRow As Long
    
        For varRow = 1 To 20
            Me.Controls("txtAmount" & varRow).Text = "whatever"
        Next varRow
        
    End Sub

  4. #4
    Forum Contributor
    Join Date
    05-09-2009
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    315

    Re: Variable Controls

    Perfect. Both worked.

    Thanks very much.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1