Consulting

Results 1 to 5 of 5

Thread: Userform: Change Textbox color in loop

  1. #1
    VBAX Regular
    Joined
    Apr 2017
    Posts
    66
    Location

    Userform: Change Textbox color in loop

    I want to loop through several textboxes on a userform and change the color of each, box names end with 1,2, 3...etc. I can change values of these boxes as follows:

    Me.Controls("TextBox_" & i).Value '<--- value works
    But variations of the above for backcolor are not working when use a counter, for example not working:

    Me.("TextBox_" & i).BackColor = vbYellow    '<--- backcolor not working
    I have seen some examples using "Got Focus" but need separate Event entry for each textbox and was hoping to avoid this if possible.

    Thanks

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Just use the same code that you use for the working .Value piece

     Me.Controls("TextBox_" & i).BackColor = vbYellow
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Regular
    Joined
    Apr 2017
    Posts
    66
    Location
    Tried that but .Backcolor , among other options are not available

  4. #4
    VBAX Regular
    Joined
    Apr 2017
    Posts
    66
    Location
    I believe the problem was related to"For Each" loop used to loop through all controls. In any case does work when use a "For" loop as below which resolves for now.

    For i = 1 To 6
        Me.Controls("TextBox_" & i).BackColor = vbYellow
    Next

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    If you were using a For Each ... loop, but trying to set"TextBox_" & i then it wouldn't work since i would have what ever was left the last time you used it
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Tags for this Thread

Posting Permissions

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