+ Reply to Thread
Results 1 to 11 of 11

Apply one code to multiple objects

Hybrid View

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

    Apply one code to multiple objects

    Hi all,

    I have built a multipage userform which holds around 50 of different objects, such as textboxes, comboboxes etc. For every single object I have written 2 codes that format that object on entry and exit. How do I make these 2 codes universal for the uniform, so that they kick in when any of 50 objects get focus? It drives me mad to go through 100 codes and do changes!

    [CODE][/Private Sub Title_Enter()
    If Title.Text = "Title" Then
    Title.Text = ""
    Title.BackColor = RGB(255, 255, 153)
    End If
    End Sub
    Private Sub Title_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    If Trim(Title.Text) = "" Then
    Title.Text = "Title"
    Title.BackColor = RGB(255, 255, 255)
    End If
    End SubCODE]

    I would be so much gratefull if someone could help me out with this issue!

    Thanks in advance!

  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

    Sorry, messed up with tags...

    Private Sub Title_Enter()
    If Title.Text = "Title" Then
    Title.Text = ""
    Title.BackColor = RGB(255, 255, 153)
    End If
    End Sub
    Private Sub Title_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    If Trim(Title.Text) = "" Then
    Title.Text = "Title"
    Title.BackColor = RGB(255, 255, 255)
    End If
    End Sub

  3. #3
    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.

  4. #4
    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...

  5. #5
    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

    Is the text in the control always the same as the name of the control, or not?

  6. #6
    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

    No, it is not.

  7. #7
    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

    Then you must pass that as an additional argument
    Private Sub Title_Enter()
        EntryFormat Title, "Title"
    End Sub
    Private Sub Title_Exit(ByVal Cancel As MSForms.ReturnBoolean)
        ExitFormat Title, "Title"
    End Sub
    Sub EntryFormat(ctl As MSForms.Control, default as string)
        If LCase$(ctl.Text) = LCase$(default) Then
            ctl.Text = ""
            ctl.BackColor = RGB(255, 255, 153)
        End If
        
    End Sub
    Sub ExitFormat(ctl As MSForms.Control, default as string)
        If Trim(ctl.Text) = "" Then
            ctl.Text = default
            ctl.BackColor = RGB(255, 255, 255)
        End If
    
    End Sub
    The enter and exit for each control will be like the first two subroutines above but passing a different control name and default text.

  8. #8
    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

    Sorry mate, I don't really get it. Are you saying it cannot be done? Or what do I need to do?

  9. #9
    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

    No I am saying that you need only repeat this part for each additional control
    Private Sub Title_Enter()
        EntryFormat Title, "Title"
    End Sub
    Private Sub Title_Exit(ByVal Cancel As MSForms.ReturnBoolean)
        ExitFormat Title, "Title"
    End Sub
    You need to change the name of the control (Title) to match each control name, and change the default text ("Title") to whatever should be used for each control.

    If you wish to adopt a leaner, class-based approach, you need something similar along the lines of post #3 here- http://www.mrexcel.com/forum/excel-q...ss-module.html

  10. #10
    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

    Oh, sorry again. I think I got it now. I still need to copy them 2 codes 50 times, but it will be easier to maintain any changes, which can be done in a subroutines.

    Isn't there a piece of code that would identify which object got focus and then apply them 2 codes?

  11. #11
    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

    Thanks mate, definitely much easier now! You made my day, thanks.

+ 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