+ Reply to Thread
Results 1 to 6 of 6

VBA Code to pull data from cell that has data validation

  1. #1
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Waterbury, CT
    MS-Off Ver
    Office 365
    Posts
    1,175

    VBA Code to pull data from cell that has data validation

    Hello,
    I would be super grateful to all who can help me with an issue i am having with one of my macros.
    When you open the attached workbook click on 'Open Userform".
    Here is a brief explanation of the userfrom:
    1) the top right combobox lists all the customers
    2) when a customer is selected then if the customer requested a brand then it will list that brand in the "Requested Brands" listbox
    3) next, if the a requested brand is selected then the applicable products are highlighted on the "Products" listbox
    4) note to consider: the userform is pulling the cell contents of the active customer and the requested brand in order to display the requested products of that brand. the cell contents are stored in a datavalidation list in the cell
    5) there are two option buttons, depending on which button is selected the BrandListBox and the ProductListBox are formatted to either act as a "MultiSelect" listbox or a "SingleSelect" listbox, then the user can "AddBrands" to the customer by either adding multiple brands to a single selected product (this will be option button #2) or by adding multiple products to a single selected brand(this will be option button #1).

    Now for the issue:
    When the first option button is set to true then the user must select a single brand to one or more products and all works well when the "Add Selected Brands & Products" is clicked, but when the 2nd option button is true then the user must select a single product to one or more brands, however, when the "Add Selected Brands..." button is clicked...the code's debugger goes off!

    *the code's debugger focuses on reading the datavalidation in the cell and seems that it is having trouble with "storing the previous data validation list into a variable named DataVal".

    I have been having issues with this for quite some time trying all possibilities i could think of. I would be truly grateful to anyone who can help me solve this issue.

    Thanks in advance
    Attached Files Attached Files
    Last edited by kosherboy; 05-14-2016 at 10:02 PM.

  2. #2
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Waterbury, CT
    MS-Off Ver
    Office 365
    Posts
    1,175

    Re: VBA Code to pull data from cell that has data validation

    I got rid of some "extra" coding to simplify it, see updated workbook attached to this post.
    All you have to do in order to to catch the bug is to open the userform, select any random customer, set the option box on the right to true, select one or more brands and select one product and hit "Add Selected Brands & Products".
    Attached Files Attached Files
    Last edited by kosherboy; 05-14-2016 at 10:02 PM.

  3. #3
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Waterbury, CT
    MS-Off Ver
    Office 365
    Posts
    1,175

    Re: VBA Code to pull data from cell that has data validation

    I ran into this problem more than two weeks ago, I've tried countless different routes and tried tweaking the code in every possible way i could think of but still it's a no-go. I am still in the process of working on it and literally won't stop till it gets resolved
    I am really determined to get this solved at any cost.

  4. #4
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,028

    Re: VBA Code to pull data from cell that has data validation

    Maybe this is the problem:
    this line:
    BrandListBox.Selected(BrandListBox.ListIndex) = False
    and
    ProdListBox.Selected(ProdListBox.ListIndex) = False

    The line is supposed to clear selections, right? But it will throw an error if nothing is selected.
    And the thing is you set the listbox MultiSelect property dinamically (so sometimes it's SelectSingle and sometimes SelectMulti ), so it's rather hard to deal with.

    Try changing those line with this (it will clear selections in the listbox):

    BrandListBox.Selected(BrandListBox.ListIndex) = False
    to this:
    Please Login or Register  to view this content.
    ProdListBox.Selected(ProdListBox.ListIndex) = False
    to this:
    Please Login or Register  to view this content.
    See this discussion:
    http://www.excelforum.com/excel-prog...selection.html

  5. #5
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Waterbury, CT
    MS-Off Ver
    Office 365
    Posts
    1,175

    Re: VBA Code to pull data from cell that has data validation

    Dear Akuini,
    Thank you so much for your response.
    Thanks for catching that issue and solving it
    However, the original issue still stands.
    Here is the code that the debugger focuses on, the script marked in red is what the debugger highlighted (please note i've changed a few things around in the code than originally posted but nothing major)
    Please note that the below code gets executed only when ProdListBox is set to single-select and BrandListBox is set to multi-select:
    Please Login or Register  to view this content.
    What's interesting to note is that if the cell already has a previous validation list then the code works well for that BrandRow, and if there is no validation then only the first loop works but the rest of the loops (if there is no validation) will bug.
    Last edited by kosherboy; 05-16-2016 at 09:22 AM.

  6. #6
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Waterbury, CT
    MS-Off Ver
    Office 365
    Posts
    1,175

    Re: VBA Code to pull data from cell that has data validation

    After many days of toil on this issue i am proud to say that this thread is finally solved!
    It occurred to me that DataVal was not properly getting nullified every time the loop repeated itself.
    I tried something like:
    Please Login or Register  to view this content.
    but same old story.
    So i left this possible reason why the code might be bugging and pursued other sources.
    Anyway, tonight as i found myself once again getting lured towards this seemingly impossible bug, i decided to revisit the idea of resetting a variable.
    I did some googling and began to research all variables and their proper reset code and this is what i found: DatVal is a string, resetting a string value involves "vbNullString", so:
    Please Login or Register  to view this content.
    note that i also had to change "On Error Goto Trap" to "On Error Resume Next"

    It's really rewarding to find a solution and i do not regret all the time I've spent because along the way i learned a lot of new concepts

+ 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. Replies: 3
    Last Post: 10-09-2014, 02:51 AM
  2. Data validation based on another cells contents
    By Baabaa in forum Excel General
    Replies: 5
    Last Post: 09-09-2014, 07:26 AM
  3. Data Validation based on contents of other cells
    By MrCraig in forum Excel General
    Replies: 2
    Last Post: 11-02-2013, 09:39 AM
  4. I need a code to retrieve data from the internet
    By dnorris707 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-07-2013, 12:51 PM
  5. [SOLVED] I need to automatically retrieve data relating to a product code upon entry.
    By Kylemarc in forum Excel Formulas & Functions
    Replies: 11
    Last Post: 08-12-2013, 09:21 AM
  6. Use a data validation list to determine another cell's contents
    By jstn in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-07-2012, 10:37 AM
  7. Find Code and Retrieve Data
    By joshw in forum Excel General
    Replies: 2
    Last Post: 01-10-2011, 06:47 PM

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