+ Reply to Thread
Results 1 to 11 of 11

Apply one code to multiple objects

Hybrid View

  1. #1
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Apply one code to multiple objects

    Hi,

    Is the code exactly the same for each control? I assume the text check is comparing the text with the name of the control.

    You might use an approach like this
    Private Sub Title_Enter()
        EntryFormat Title
    End Sub
    Private Sub Title_Exit(ByVal Cancel As MSForms.ReturnBoolean)
        ExitFormat Title
    End Sub
    Sub EntryFormat(ctl As MSForms.Control)
        If LCase$(ctl.Text) = LCase$(ctl.Name) Then
            ctl.Text = ""
            ctl.BackColor = RGB(255, 255, 153)
        End If
        
    End Sub
    Sub ExitFormat(ctl As MSForms.Control)
        If Trim(ctl.Text) = "" Then
            ctl.Text = ctl.Name
            ctl.BackColor = RGB(255, 255, 255)
        End If
    
    End Sub
    It is possible to implement handlers for the Enter and Exit events with a class but it is not at all trivial.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  2. #2
    Forum Contributor ABSTRAKTUS's Avatar
    Join Date
    04-18-2010
    Location
    England
    MS-Off Ver
    Win10 Excel 2016
    Posts
    609

    Re: Apply one code to multiple objects

    Hi xlnitwit and thanks for your response.

    Code is very similar to every control, apart from control name textbox1, 2, 3 etc. Also, the text in each control is different. The text in a control is a replacement of a label and saving space on a userform. For example textbox1 holds text "Title" in it until textbox gets focus. When it gets focus text disappears and backcolor is changed to highlight the control.

    I have been searching web for a solution, but gave up now, hence asking you guys for a help. Also, I'm a very beginner in VBA, so please bear with me...

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Apply VBA code to multiple cells
    By rosenina18 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-14-2024, 08:07 AM
  2. Wants to apply vba code to multiple cell
    By alkesh4u in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-19-2016, 10:47 AM
  3. Apply VBA code to multiple sheets
    By PaulusKabouter in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-26-2013, 11:37 AM
  4. Apply VBA code to multiple cells
    By jond291 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 05-31-2013, 07:33 AM
  5. [SOLVED] Can i modify this code to apply to multiple checkboxes and sheets?
    By stolen_83 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-14-2013, 02:44 PM
  6. [SOLVED] Match code to apply multiple cells in 1 Sub
    By colvinb in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-20-2012, 06:38 AM
  7. Need to apply VBA code to multiple Worksheets
    By parteegolfer in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-12-2006, 04:43 PM

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