+ Reply to Thread
Results 1 to 8 of 8

VBA data validation, dependent dropdown list

  1. #1
    Registered User
    Join Date
    07-12-2013
    Location
    Austrlia
    MS-Off Ver
    Excel 2003
    Posts
    7

    Question VBA data validation, dependent dropdown list

    Hi everyone,

    I'm working on an excel that consists of a fill-in form within the worksheet. I understand it may be better to use a userform and activeXcontrols, but I already have 2 userform in the workbook.
    *The workbook needs to be usable by all users, therefore I can't use plug-ins.
    *I'm totally new in VBA, so please avoid "refer to this link" only. I won't be able to understand the code unless it has comments. I need to know where and which part I can/cannot change.

    I need to generate a dependent dropdown list. Which I managed to have it working, however, (as attached) I need to place the reference value in Cell M2. Is there possibly a method to not having to display a value in cell M2 and still have it working?

    Notes:
    1. information of column [F, G] and [I,J,K] is not constant, as user can add new "company", "product", "detail" to list
    2. the actual lists is separated into different worksheet (ie. sheet1 = in-sheet form, sheet2 = company list, sheet 3 = product list)
    3. reason that I'm doing this in VBA and not excel built-in functions is to avoid formula loss (overwriting the data without realizing it and then saving the workbook [which happened before])

    Possible Error:
    I realized that if the list from column I to K is not sorted according to column I, the drop down list will generate mismatch

    For example
    Col I Col J
    A apple
    A orange
    B Beef
    A Lemon

    with this list above, the dependent drop down list for A will be (apple, orange, BEEF). It can count there are 3 "A" but the results are shifted.

    I appreciate for your help!


    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by revzephyr; 03-11-2014 at 09:55 AM.

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: VBA data validation, dependent dropdown list

    Hi,

    It looks like you are wanting to learn Cascading Validation Lists. Read about them at:
    http://www.excel-user.com/2011/02/ca...ion-lists.html

    Because you want to add to data it also seems you need more than Named Ranges. There are these magic things called Dynamic Named Ranges that may help in your puzzle.
    Read about them at:
    http://www.bettersolutions.com/excel...G820716330.htm

    I hope the above topics will help with your problem. Note - No VBA required for either of the above topics, as you requested.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: VBA data validation, dependent dropdown list

    Quote Originally Posted by revzephyr View Post
    ... I need to place the reference value in Cell M2. Is there possibly a method to not having to display a value in cell M2 and still have it working?
    If you don't use cell M2, how would the user select a company?

    Edit: disregard. I'm a little slow tonight.
    Last edited by AlphaFrog; 03-01-2014 at 11:59 PM.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  4. #4
    Registered User
    Join Date
    07-12-2013
    Location
    Austrlia
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: VBA data validation, dependent dropdown list

    I was thinking that possibly (not sure if it is possible, and as far as I have tried it didn't work), temporarily store the information as a string...

    Dim selected_company As String
    elected_company = Application.WorksheetFunction.VLookup(.Range("A2"), .Range("F:G"), 2, False)

    ...and then somehow come the string during "offset"?

    the usual: "=OFFSET(OFFSET(I:I,0,1,1,1),MATCH(M2,I:I,0)-1,0,COUNTIF(I:I,M2))"
    string version?: "=OFFSET(OFFSET(I:I,0,1,1,1),MATCH(" &selected_company&",I:I,0)-1,0,COUNTIF(I:I,"&selected_company&"))"

    OR

    if there is a method where I still store the vlookup result in the worksheet, but it is not visible to the user

  5. #5
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: VBA data validation, dependent dropdown list

    Try this.

    For the B2 Data validation list formula use this.
    =INDIRECT("DetailA")

    And this for C2
    =INDIRECT("DetailB")

    This will use two named ranges as their list ranges


    The code below will update the DetailA and DetailB named ranges when you make a change to A2.

    You could also put the lists on other sheets and edit the code to those sheet names and ranges. When the form-sheet is activated, the named ranges are updated to reflect any changes the user may have made.


    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    07-12-2013
    Location
    Austrlia
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: VBA data validation, dependent dropdown list

    AlphaFrog, thank you very much. I just give it a test and it works great. However, if I select "company" (as in F1) it will trigger an error for debug. Is there a method to name a range which can avoid this debug?

    error code:
    Please Login or Register  to view this content.
    once again, thank you very much. I appreciate it.

  7. #7
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: VBA data validation, dependent dropdown list

    "company" isn't a company; It's a header. There is no detail data associated with it.

    Change the DV list in A2 so it doesn't have "company" in the list. Problem solved.

  8. #8
    Registered User
    Join Date
    07-12-2013
    Location
    Austrlia
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: VBA data validation, dependent dropdown list

    AlphaFrog

    First of all, thanks for the help. Now the major problem is solve it is down to details.
    If you are still available to help, it would be great.

    PART 1)
    Referring back to the attached file before,
    (Column F) Existing Company is A, B, C. and for each exiting company there is at least 1 detail (Column J).
    However, if there is a company D but it doesn't have any detail (Column J) yet, the data validation result will be result from last entry.

    Example
    situation: Given then Company D is added but has no detail referred to it yet.

    Column A Column B
    Company B details for company B
    Second Entry after... choosing "company B"
    Company D details for company B

    Sadly, this produce a incorrect findings. Is there a way to add something like

    if range("A2")'s result = "" then range("B2").validation.delete
    OR
    if range("B2")'s result = "" then _
    range("B2").validation.delete
    msgbox "none matching results found", vbOKonly

    PART 2)
    Is there any way to have the data validation index to start at 0 on thisworkbook_open?

    Thank you 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] Dependent dropdown validation - dependent data is string
    By jnewby in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 02-12-2014, 07:50 PM
  2. Data Validation List - Dependent on Adjacent Cell - Value from Unsorted List
    By justforthis1 in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 03-26-2013, 12:24 PM
  3. Replies: 2
    Last Post: 07-17-2012, 01:18 PM
  4. 4 way dependent dropdown/data validation
    By Cidona in forum Excel - New Users/Basics
    Replies: 2
    Last Post: 02-19-2012, 06:58 AM
  5. Excel 2007 : Dependent Dropdown (Data Validation)
    By mtpr220 in forum Excel General
    Replies: 16
    Last Post: 08-10-2011, 12:58 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