Results 1 to 2 of 2

Thread: [VBA] Reference to commnadbutton's property without directly using its name
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    Board Regular
    Join Date
    Jul 2015
    Posts
    361
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default [VBA] Reference to commnadbutton's property without directly using its name

    I have a around 10 cFmtCell commandbuttons on a userform.
    Instead of repeating the following code for 10 times, with varying indices at the end, is there a more convenient way to write the code?



    Code:
    Private Sub cFmtCell7_Click()
        Call FmtCellTxt(cFmtCell7.BackColor, vbWhite)
        Unload Me
    End Sub
    
    Private Sub cFmtCell8_Click()
        Call FmtCellTxt(cFmtCell8.BackColor, vbWhite)
        Unload Me
    End Sub
    
    Private Sub cFmtCell9_Click()
        Call FmtCellTxt(cFmtCell9.BackColor, vbWhite)
        Unload Me
    End Sub

    Thanks a lot!

  2. #2
    MrExcel MVP
    Moderator
    RoryA's Avatar
    Join Date
    May 2008
    Location
    UK
    Posts
    33,815
    Post Thanks / Like
    Mentioned
    48 Post(s)
    Tagged
    6 Thread(s)

    Default Re: [VBA] Reference to commnadbutton's property without directly using its name

    You can use a class module to handle multiple controls that do the same thing. You'll find numerous examples on the forum.

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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