+ Reply to Thread
Results 1 to 3 of 3

Looking to simplify code for generic controls on a UserForm

  1. #1
    Registered User
    Join Date
    08-20-2020
    Location
    Houston, Texas
    MS-Off Ver
    2016 Excel
    Posts
    16

    Looking to simplify code for generic controls on a UserForm

    For my specific case I am using Option Buttons to select a sort field column from a table. Wishing to make the UserForm and controls generic so the basic code could be reused in various applications, I placed 20 Option Buttons on the form. I manually copied column headings from my "Customer" table to a "Customer Sort Fields" sheet, and then placed an 'x' in the row below the headings for each field on which I might wish to sort. The first part of the code below successfully changes the Option Button caption to the selected column heading, and the second part hides unused Option Buttons. This works fine. But is there an easier way? I tried using a variable for "OptionButtonX" method but that doesn't work (and I didn't believe that it would.) Any thoughts?


    Please Login or Register  to view this content.

  2. #2
    Valued Forum Contributor
    Join Date
    11-04-2018
    Location
    Denpasar
    MS-Off Ver
    Excel 2010
    Posts
    777

    Re: Looking to simplify code for generic controls on a UserForm

    Sorry, this is not helping your situation...
    but maybe you can tweak the code to meet your situation.

    Please Login or Register  to view this content.
    The condition as follow :
    1. rgHeader is the range of the header - rgX is the range one row below the rgHeader
    2. there are OptionButtons in the userform as follow
    OptionButton1
    OptionButton2
    OptionButton3
    .... until
    OptionButton20

    3. then the code loop to only the existing value of "x" in the range of rgX and get the name of the column of the header above the "x" (the nm variable),
    4. then loop to each existing control
    4. if the control is OptionButton and if the caption is "OptionButton(i)"
    5. then it change the "OptionButton(i)" caption to variable nm value, then jump to the outside loop
    6. finally the other loop is to hide the rest of the OptionButton which caption has a text "OptionButton".
    FYI, there must be NONE [OptionButton with the caption has a text "OptionButton"] which is not meant for the header.

    As I'm not an expert and limited in English language,
    maybe this is not what you want.

    BTW, another way is not using OptionButton, but a ComboBox.
    The ComboBox will get the item as many as there are the "X" under the row of the columns header.
    So there is no "hiding" process.
    Or create the OptionButton at run time, user form initialize.
    Last edited by karmapala; 09-18-2021 at 12:55 AM.

  3. #3
    Registered User
    Join Date
    08-20-2020
    Location
    Houston, Texas
    MS-Off Ver
    2016 Excel
    Posts
    16

    Re: Looking to simplify code for generic controls on a UserForm

    karmpala,

    Perfect! Yes, this is exactly what I want! And so much simpler than mine. Thanks very much!

+ 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] Inserting image into userform controls strictly through code
    By DJams in forum Excel Programming / VBA / Macros
    Replies: 42
    Last Post: 11-24-2023, 03:55 AM
  2. [SOLVED] how to simplify setting controls in code
    By dmcgov in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-17-2020, 12:27 PM
  3. [SOLVED] Cleaner code for looping through userform controls (beginner)
    By melpa in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-20-2019, 08:07 AM
  4. [SOLVED] Choosing particular Userform controls array in a line of code.
    By pipoliveira in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-20-2018, 02:04 PM
  5. [SOLVED] Simplify VB code from UserForm to save it in excel sheet Cell
    By 23inzane10 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 07-19-2016, 02:34 AM
  6. Code for userform controls
    By Damperman in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 05-10-2013, 01:56 PM
  7. Deleting Userform controls through code (Help)
    By stuart010 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-26-2011, 10:12 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