+ Reply to Thread
Results 1 to 6 of 6

Combo Box code for dependent data validation using a table for validation data

  1. #1
    Registered User
    Join Date
    08-20-2011
    Location
    Geneva, Switzerland
    MS-Off Ver
    Excel 2010
    Posts
    9

    Combo Box code for dependent data validation using a table for validation data

    Hi everyone.

    First, sorry, I've not made enough posts to be able to put links in here, so I'll try obfuscating them.

    I have a lot of excel files with tables in multiple sheets populated from external sources, and then manually categorised, and subcategorised. As you can imagine, the categories and subcategories are using data validation lists. My dependent validation data is always stored as a table - the Categories are headers, and the subcategories are the columns. The data can be multiword, and sometimes uses the "&" symbol.

    Given the limitations of the built-in drop-down menu, I'd like to use a Combo Box instead. I've never used VBA in Excel (although I've programmed in languages like FORTRAN, C++, Perl, etc. since the 70s).

    Looking for dependent data validation list Combo Box code, I found this page "contextures.com / xldataval11.html"
    and this thread "1194306-data-validation-dynamic-drop-down-with-combo-box-indirect-not-working.html" with code for the validation data being in named ranges, but nothing when the validation data is in a table. Here's the code from that post:
    Please Login or Register  to view this content.
    I'm including a sample file of the type of file I have. I'd appreciate any help in modifying the code to use validation data from a table instead of named ranges, so I can make minimal changes to the files (adding a Combo Box and code) with having to transform all of the validation tables into names ranges as well.
    Attached Files Attached Files

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Combo Box code for dependent data validation using a table for validation data

    It seems quite complicated code if all you want to do is populate several combo boxes with values from several lists.

    First of all don't reject the idea of using range names. Dynamic range names which expand or contract as new items are added or deleted will ensure that wherever you use the range name it will cover all your values. For example to create dynamic range for the Food create a name called say lst_Food and define it with the formula

    =OFFSET(ValidationTable!$A$5,0,0,COUNTA(ValidationTable!$A5:$A100),1)

    Then use the 'RowSource' property of the CombBox and enter the name 'lst_food'
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Registered User
    Join Date
    08-20-2011
    Location
    Geneva, Switzerland
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Combo Box code for dependent data validation using a table for validation data

    Quote Originally Posted by Richard Buttrey View Post
    It seems quite complicated code if all you want to do is populate several combo boxes with values from several lists.

    First of all don't reject the idea of using range names. Dynamic range names which expand or contract as new items are added or deleted will ensure that wherever you use the range name it will cover all your values. For example to create dynamic range for the Food create a name called say lst_Food and define it with the formula

    =OFFSET(ValidationTable!$A$5,0,0,COUNTA(ValidationTable!$A5:$A100),1)

    Then use the 'RowSource' property of the CombBox and enter the name 'lst_food'
    The problem is that I have a lot of existing workbooks, often with multiple tables of data for data validation, so I'd really like to find a solution to this that allows me to minimize the job of updating those files.

  4. #4
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Combo Box code for dependent data validation using a table for validation data

    Quote Originally Posted by [email protected] View Post
    The problem is that I have a lot of existing workbooks, often with multiple tables of data for data validation, so I'd really like to find a solution to this that allows me to minimize the job of updating those files.
    Since this seems a one off task and once the names have been created I find it's sometimes quicker to bite the bullet and do the job manually than spend tine writing an debugging a macro that will only be used once in any workbook

    Otherwise write an initial macro that first loops through all your lists and creates the dynamic range names, and which then goes on to populate the combobox RowSource properties with the range name.

  5. #5
    Registered User
    Join Date
    08-20-2011
    Location
    Geneva, Switzerland
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Combo Box code for dependent data validation using a table for validation data

    Quote Originally Posted by Richard Buttrey View Post
    Since this seems a one off task and once the names have been created I find it's sometimes quicker to bite the bullet and do the job manually than spend tine writing an debugging a macro that will only be used once in any workbook

    Otherwise write an initial macro that first loops through all your lists and creates the dynamic range names, and which then goes on to populate the combobox RowSource properties with the range name.
    Thanks for your advice, which I'll consider, but a quick calculation shows I have about 127 files to update, with about 4 tables of validation table each. In the case of trying to automate the task, the question arises of how to name the ranges. Solutions I've seen for Combo Boxes rely on the name of the dependent range being the same as the value of the field, which won't work with a value like "Revenue & Expense" as it is not a valid name.

    I'd also like to have the solution for future files I create, so I think I'll persevere in trying to find a solution using tables.
    Last edited by [email protected]; 01-15-2022 at 12:55 PM.

  6. #6
    Registered User
    Join Date
    08-20-2011
    Location
    Geneva, Switzerland
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Combo Box code for dependent data validation using a table for validation data

    Well, I learnt a useful bit of VBA code (Debug.Print) and found that the issue is not a difference of using named ranges vs tables, but limitations in the code to deal with complex data validation list formulas.

    On such formulas, the code breaks at this point:
    Please Login or Register  to view this content.
    On the simple code used for taking the contents of the cell to the left as the name of a range, .ListFillRange ends up as an absolute range.

    With the formulas I've been using for tables, however, the nm.RefersToRange.Parent.Name causes an issue.

    A value of nm from a named range (with substitutes to create the name) that works at this point is
    Please Login or Register  to view this content.
    A table one that breaks has a value of nm at this point:
    Please Login or Register  to view this content.
    The main difference between them is the R1C1 indirect, but having embedded named formulas with also break it.

    As a hack, the following validation list formula will work with the code for tables.
    Please Login or Register  to view this content.
    So, instead using an A1 reference instead of using R1C1 indirect, it is possible to use tables instead of named ranges.
    For me, the advantage is that it is easier to maintain a table, rather than named ranges.

    A more eloquent solution would be to modify the code so that it also bottoms out to an absolute range.

+ 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. Dependent data validation list in 2 column table
    By jaryszek in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 12-21-2018, 06:54 AM
  2. Replies: 1
    Last Post: 01-14-2018, 11:04 AM
  3. Data Validation Combo Box with Multiple Independent Validation Lists
    By firstofnine in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-24-2016, 04:38 PM
  4. Replies: 0
    Last Post: 10-10-2015, 09:51 AM
  5. Using a combo box with dependent data validation
    By calberts in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-03-2013, 03:09 PM
  6. Combo box code instead Data validation
    By suhi in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-24-2011, 11:46 AM
  7. Code to refresh dependent data validation list
    By penfold in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-01-2009, 03:01 PM

Tags for this Thread

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