+ Reply to Thread
Results 1 to 2 of 2

How do i avoid repetitive codes when i have a userform with many varying options?

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-03-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    107

    How do i avoid repetitive codes when i have a userform with many varying options?

    For example i have this userform with 3 comboboxes.

    let them be combobox_dpfs_system, combobox_dpfs_af, combobox_dpfs_accessory.

    combobox_dpfs_system has 2 choices: "System 1" or "System 2"
    combobox_dpfs_af has 4 choices: "100 AF", "200 AF", "300 AF", "400 AF"
    combobox_accessory has 2 choices: "Pilot Light", "None"

    so when the user makes the selections, he/she presses a command button and the code will call up data from a sheet depending on the selections. Here is my problem. An example code i did was something like this:

    if combobox_dpfs_system = "System 1" And _
    combobox_dpfs_af = "100 AF" and_
    combobox_accessory = "None" Then
    
    textbox1 = workbooks("Estimating Program 2014").Sheets("Kanye West").Cells(1,1)
    textbox1 = workbooks("Estimating Program 2014").Sheets("Kanye West").Cells(2,2)
    end if
    
    
    if combobox_dpfs_system = "System 2" And _
    combobox_dpfs_af = "100 AF" and_
    combobox_accessory = "None" Then
    
    textbox1 = workbooks("Estimating Program 2014").Sheets("Kim Kardashian").Cells(1,1)
    textbox1 = workbooks("Estimating Program 2014").Sheets("Kim Kardashian").Cells(2,2)
    end if

    So i would do numerous if-then statements until i cover all the possibilites of the 3 combobox combinations. It is very tedious and i dont think this is the best way of doing it. Is there a way where there will be one IF-then main procedure and only variables will be changed? I really don't know I need help... Thanks...

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: How do i avoid repetitive codes when i have a userform with many varying options?

    Hi kjy,

    You might want to put a logic table in a spreadsheet containing all the valid choices in your 3 combo boxes such as:
    Column A - Combo Box 1 - system
    Column B - Combo Box 2 - af
    Column C - Combo Box 3 - accessory

    Column D - Sheet Name
    Column E - Something else chosen when A,B,C match

    You would have several rows. When columns A, B, and, C match, then you use the item in Column D
    (and possibly, E...) to do whatever you need to do. All the logic would be in the data tables, and not in the code. If there were impossible combinations, then they would not appear in the table, and you would have a 'NO MATCH' situation.

    Lewis

+ 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. Image on userform codes
    By Mehmet82 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-27-2013, 08:22 AM
  2. [SOLVED] How to avoid running userform if there's not date on particular column?
    By unley in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-23-2013, 12:33 AM
  3. how to make a list to avoid repetitive texts
    By Elainefish in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-09-2013, 08:37 AM
  4. Replies: 3
    Last Post: 07-12-2011, 02:52 PM
  5. Creating a varying UserForm using cells from a hidden worksheet
    By Mort 62 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-11-2005, 10:05 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