- Community Pick
- Experts Exchange Approved
- Editor's Choice
David Miller (dlmille)
Intro
This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Data Validation (DV) lists.
This is the Part II article, I promised, as a follow-on to Drop Down List with Unique/Distinct Values (Enhancing the ComboBox with a few steps and a little code) http:\A_5062.html,, where I've modified the code to support OLE Objects (Active-X Controls) that support lists (e.g., ComboBox and ListBox are the practical examples, today). In addition, I've taken some of these learnings and applied them to a related Contextures tip http://www.contextures.com
In each example – list object (ComboBox, ListBox) or DV list – I’ve leveraged the use of the dictionary (see matthewspatrick’s related article http:\A_3391.html) and a sort to deliver a unique list to these controls, based on an initial setup of a listfill range (or DV List range).
I’m not going to go into detail on the first part (ComboBox and ListBox) as the documentation in Part I of this Article, http:\A_5062.html, achieves that end. Suffice it to say, I leveraged the same routines, “objectivizing” my variables such that either ComboBox or ListBox could be used interchangeably, based on preference. I’ve also leveraged “late binding” for the Dictionary class, to ease deployment without the need to explicitly add the appropriate reference library. The code that achieves this transformation is posted (along with demo spreadsheet add-in) at the bottom of this Article.
Data Validation (DV) Lists - Extended
The remainder of this write-up will focus on the Data Validation (DV) list, and how, with a few simple steps and a little code, we can dynamically deliver unique sorted lists for general use in any workbook spreadsheet.
How to install and use the Dynamic DV! Add-in.
At the bottom of this article, there’s a second posting for the Dynamic DV! Add-in. With the add-in loaded, any data validation in any workbook that references a range for its reference list will have access to the features of Dynamic DV! Note: Dynamic DV! depends on the Worksheet_Change() event, so if for some reasons events become disabled, just select the Dynamic DV! menu on the Add-ins tab and choose the Initialize option to reinitialize the event class for the tool, and re-enable Events.
- 1
Starting with a new/existing workbook
Let’s create a new workbook with a defined range of items that are not sorted in any particular order:
Once the blank workbook is created (or any existing workbook is opened), a click of the button on the worksheet initiates code to deposit a hidden temporary ComboBox on the active sheet. Just hit OK. Dynamic DV! Is now enabled on this worksheet.Note: the hidden TempCombo ComboBox is made visible during use, and is loaded with data, dynamically, as needed.
- 2
Using new/existing DV Lists
With the new blank workbook created, we can then build our test range and data validation list. I started a miscellaneous tracking list: Customer, Products, Billing (etc…), selected the first open cell and built a DV list on the range I created:
I shaded the DV cell grey to make it easier to identify, then copied it down to row 21. For demonstration purposes, I’ve turned off the Add-in for the next comment. Note the DV list shows the data in the range we created, unsorted and with duplicates:
- 3
Testing the Dynamic DV! Add-in
Now, let’s re-activate the Add-in and see what happens when we click on a DV cell:
And that’s all there is to it! Create as many DV Lists as you like, pointing to whatever valid ranges that exit, and Dynamic DV! will consistently deliver distinct/unique, sorted list.
Note: there are two dropdown arrows in the above view. One for the ComboBox, and one for the DV List. It’s simple enough to eliminate the DV list’s dropdown arrow, by de-selecting it in the Data Validation toolbar:
How it works
When selections are made on the worksheet, a Worksheet_Change() event is triggered. The cell is then interrogated to determine if it is a data validation cell with a list and a valid range. The temporary ComboBox object TempCombo is then loaded with a unique, sorted list of items from that range, using the previously discussed (see Part I: http:\A_5062.html) sub loadMyListObjectUnique()
Here’s the code for trapping that event and delivering the DV list in the fashion described, which is stored in the ThisWorkbook codepage (see below notes for add-in):
And here’s the DataValidationManagement()
As previously mentioned, and noted in the code, I started with an excellent code snippet from Contextures http://www.contextures.com
Attachment I: Attached, please find an all-in-one demonstration workbook that contains the entire codeset, supporting dynamic dropdown lists through list objects (e.g., ListBox, ComboBox) and DV lists. The example workbook has several examples for you to discover opportunities for your next project:
Attachment II v2.0: Attached, please find the Dynamic DV! Add-In to easily add this functionality to any existing project with a click of a button. Also attached, please find the Demo spreadsheet used in this article. You can start using Dynamic DV! today:
Thanks to Feedback from others, especially Jfiestere and Bright01, I've incorporated their constructive feedback into these enhancements.
Enhancements include:
2. AutoFill bug fixed, now you can start typing and see the text autofill.
3. If you're editing a word in DynamicDV! and hit ESC, the prior value reverts to the cell
4. Added Data Validation checking (yes, it needed it;) with a prompt if the value entered is not in the list (careful, if you DV list includes a blank cell, it will never alert, even with normal DV!)
5. Note, when you are backspacing on a word in the combo box, the cells are highlighted which is a difference from an Excel normal cell or DV cell. I haven't modified this "feature", as you can just hit the delete key when you want the word to be truncated.
I also added an auto install function for the add-in. With Excel already started, just download the new add-in version 2.0 and run it, after a couple prompts, it should be installed.
There is a revised Add-in menu for DynamicDV! that is fairly self explanatory. You can turn it on/off for your current Excel session, and you can toggle the startup state (e.g., when you start Excel whether Dynamic DV is automatically on or off.) Finally, I also added a ToggleValidationAlert to turn on/off the validation "feature".
Attachment III: Original Dynamic DV!
Attachment IV:Demo example workbook used in this article.
Enjoy!
David Miller (dlmille)
by: mwvisa1 on 2011-06-27 at 10:06:10ID: 29206
Thanks and I voted YES here too. Appreciated as much as part I.