+ Reply to Thread
Results 1 to 5 of 5

loop through concatenated object names and variables

Hybrid View

  1. #1
    Registered User
    Join Date
    12-30-2009
    Location
    Florida
    MS-Off Ver
    Excel 2007
    Posts
    43

    loop through concatenated object names and variables

    So I have the following code which works:

        SL_TB1.ControlSource = "Calculations!AG3"
        SL_TB2.ControlSource = "Calculations!AG4"
        SL_TB3.ControlSource = "Calculations!AG5"
        SL_TB4.ControlSource = "Calculations!AG6"
        SL_TB5.ControlSource = "Calculations!AG7"
        SL_TB6.ControlSource = "Calculations!AG8"
        SL_TB7.ControlSource = "Calculations!AG9"
        SL_TB8.ControlSource = "Calculations!AG10"
        SL_TB9.ControlSource = "Calculations!AG11"
        SL_TB10.ControlSource = "Calculations!AG12"
        SL_TB11.ControlSource = "Calculations!AG13"
        SL_TB12.ControlSource = "Calculations!AG14"
        SL_TB13.ControlSource = "Calculations!AG15"
        SL_TB14.ControlSource = "Calculations!AG16"
        SL_TB15.ControlSource = "Calculations!AG17"
        SL_TB16.ControlSource = "Calculations!AG18"
        SL_TB17.ControlSource = "Calculations!AG19"
        SL_TB18.ControlSource = "Calculations!AG20"
    I'd like to simplify this but can't seem to figure out how. So far I have the following code which DOES NOT work.

    For Count = 1 To 18
        
        SLTextbox = "SL_TB" & Count
        SLControlSource = "Calculations!AG" & Count + 2
        
        SLTextbox.ControlSource = SLControlSource
        
        Next Count
    Any help would be appreciated, thanks.
    Last edited by TYork; 06-24-2010 at 04:32 PM.

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,861

    Re: loop through concatenated object names and variables

    I think something like this should work but to test it out I would need to know where are your text boxes--in a worksheet, or on a form? And are you using ActiveX or Form controls?

       Dim c as Object
       For Each c In Me.Controls
          If Mid(c.Name, 1, 5) = "SL_TB" Then
             i = Mid(c.Name, 6)         
             c.ControlSource = Range("Calculations!AG" & i + 1)
          End If
       Next c
    Last edited by 6StringJazzer; 06-24-2010 at 02:54 PM. Reason: changed declaration from Control to Object
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    12-30-2009
    Location
    Florida
    MS-Off Ver
    Excel 2007
    Posts
    43

    Re: loop through concatenated object names and variables

    The textboxes are on a userform and I am using activex controls.

  4. #4
    Registered User
    Join Date
    12-30-2009
    Location
    Florida
    MS-Off Ver
    Excel 2007
    Posts
    43

    Re: loop through concatenated object names and variables

    Your original code works, although "Range()" need to be removed from around "Calculations!AG" & i + 1

    Thanks for your help

  5. #5
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,861

    Re: loop through concatenated object names and variables

    Quote Originally Posted by TYork View Post
    Your original code works, although "Range()" need to be removed from around "Calculations!AG" & i + 1

    Thanks for your help
    Sorry for the glitch. I did a generic test and not one exactly like your situation. I edited my code afterwards to put that in. Glad you were able to work around it.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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