+ Reply to Thread
Results 1 to 8 of 8

Using Sub Name in Code

  1. #1
    Forum Contributor
    Join Date
    07-07-2017
    Location
    USA
    MS-Off Ver
    2010
    Posts
    500

    Using Sub Name in Code

    Greetings,
    I have several ActiveX Control Check Boxes I need to create, all using the same code. I can copy and paste the Sub name into the code, but this will take a very long time, not to mention the chance of errors. I have tried using a DIM statement to insert the Sub name into the code, but cannot figure it out.
    Below is the code i am using.
    Please Login or Register  to view this content.

  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,914

    Re: Using Sub Name in Code

    Are you saying that you have several check boxes and you want them all to have code for a click event that is exactly the same? First, I am wondering if your design is optimal, and would like more insight to your overall design. Can you attach your file?

    But to answer the question, one way to do this is to create a class with a method, bind all your checkboxes to that class, and then the same Sub will be called when any of the checkboxes are clicked. I have attached a model that shows how to do this.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Forum Contributor
    Join Date
    07-07-2017
    Location
    USA
    MS-Off Ver
    2010
    Posts
    500

    Re: Using Sub Name in Code

    6StringJazzer,
    Thank you much for the sample workbook!
    I apologize, the check boxes will not run the same code. However, there are several lines that are the same. I am wanting to "copy and paste" the Sub name into the lines of code.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Using Sub Name in Code

    Have you considered using Forms controls?

    With those you could use the same macro for each control and use Application.Caller to handle the lines that differ for each control.
    If posting code please use code tags, see here.

  5. #5
    Forum Contributor
    Join Date
    07-07-2017
    Location
    USA
    MS-Off Ver
    2010
    Posts
    500

    Re: Using Sub Name in Code

    Norie,
    In my code below, I am using the Sub name to control the colors and value. Is there a way to insert the Sub name into these lines?

    Please Login or Register  to view this content.

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

    Re: Using Sub Name in Code

    brentjohn, are your checkboxes on a UserForm, or on a worksheet?

  7. #7
    Forum Contributor
    Join Date
    07-07-2017
    Location
    USA
    MS-Off Ver
    2010
    Posts
    500

    Re: Using Sub Name in Code

    They are on a worksheet.

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

    Re: Using Sub Name in Code

    Then you should explore Norie's suggestion of using Forms controls instead of ActiveX.

+ 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. edit in code for populate range of cell in list box and code for delete any items
    By mazan2010 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-11-2017, 03:33 PM
  2. [SOLVED] Need help with VBA code. Tons of code seperated in two, second part of code doesn't work.
    By FragaGeddon in forum Excel Programming / VBA / Macros
    Replies: 20
    Last Post: 11-30-2015, 11:46 AM
  3. [SOLVED] Pattern Building VBA Code - Working code, would like to use cleaner code
    By Benisato in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-10-2015, 03:05 PM
  4. [SOLVED] Excel VB code. Message pops up while code running asking question. Code must not wait.
    By Heinrich Venter in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-28-2014, 06:10 AM
  5. [SOLVED] VBA code for assigning a numeric code to text; then numeric code populates table
    By cteaster in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-14-2014, 08:01 PM
  6. Replies: 2
    Last Post: 03-09-2013, 04:30 AM

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