Results 1 to 12 of 12

Thread: [RESOLVED] Excel drop down lists

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2005
    Location
    Arlington, TX
    Posts
    60

    Resolved [RESOLVED] Excel drop down lists

    I know it's possible to turn a cell into a drop down list manually by going through the options, but I was wondering if anyone knew of a way to create the drop down list using code.

  2. #2
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Excel drop down lists

    If you mean the Filter option in the Data Menu.. then

    Select the cell to where you to apply the filter and then

    VB Code:
    1. ActiveCell.AutoFilter

    You can also set it with the Range object..

    For further information and the parameters look AutoFiter up in either the Object Browser (F2) in the VBE (ALT+F11)
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  3. #3

    Thread Starter
    Member
    Join Date
    Apr 2005
    Location
    Arlington, TX
    Posts
    60

    Re: Excel drop down lists

    Close, but that's not quite what I'm aiming for...what I'm looking for is in the Validation option in the Data menu.

    In this report I'm working on, the user needs to type data in, which is then used in another macro I've made to get the totals. The only problem is, since I can't watch over the people who will have to take over this report, and no one can protect against typos, I want to make a drop down list so that they only have the choices that I program in.

  4. #4
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Excel drop down lists

    Yes you can in the same format as above...

    VB Code:
    1. ActiveCell.Validation

    Extract from Excel VBA Help

    Using the Validation Object
    Use the Validation property to return the Validation object. The following example changes the data validation for cell E5.
    VB Code:
    1. Range("e5").Validation _
    2.     .Modify xlValidateList, xlValidAlertStop, "=$A$1:$A$10"
    Use the Add method to add data validation to a range and create a new Validation object. The following example adds data validation to cell E5.
    VB Code:
    1. With Range("e5").Validation
    2.     .Add Type:=xlValidateWholeNumber, _
    3.         AlertStyle:=xlValidAlertInformation, _
    4.         Minimum:="5", Maximum:="10"
    5.     .InputTitle = "Integers"
    6.     .ErrorTitle = "Integers"
    7.     .InputMessage = "Enter an integer from five to ten"
    8.     .ErrorMessage = "You must enter a number from five to ten"
    9. End With
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  5. #5

    Thread Starter
    Member
    Join Date
    Apr 2005
    Location
    Arlington, TX
    Posts
    60

    Re: Excel drop down lists

    VB Code:
    1. With Range("e5").Validation
    2.     .Add Type:=xlValidateWholeNumber, _
    3.         AlertStyle:=xlValidAlertInformation, _
    4.         Minimum:="5", Maximum:="10"
    5.     .InputTitle = "Integers"
    6.     .ErrorTitle = "Integers"
    7.     .InputMessage = "Enter an integer from five to ten"
    8.     .ErrorMessage = "You must enter a number from five to ten"
    9. End With
    Do you think you could explain this in a little more detail? I just recently started using VBA, and until today, I haven't even worked with validation through the menu....

  6. #6
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Excel drop down lists

    I'll try but this was taken from the VBA help file in Excel..

    VB Code:
    1. 'The with object allows you to use a object and it's properties by pressing .
    2. With Range("e5").Validation
    3.      'we are adding a validation rule to the spreadsheet at one cell..
    4.     .Add Type:=xlValidateWholeNumber, _ 'Specifies the whole cell content to check
    5.         AlertStyle:=xlValidAlertInformation, _ 'this is the alert style that will be shown
    6.         Formula1:="5", Formula2:="10" 'This is the range of values that are valid
    7.     'The next four objects are the message settings to be returned to the user
    8.     .InputTitle = "Integers"
    9.     .ErrorTitle = "Integers"
    10.     .InputMessage = "Enter an integer from five to ten"
    11.     .ErrorMessage = "You must enter a number from five to ten"
    12. End With

    The code was slightly wrong

    The easiest way to understand this is to create a sub in a module and then place the code.. next attempt to use the cell to which the validation is being set..
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  7. #7

    Thread Starter
    Member
    Join Date
    Apr 2005
    Location
    Arlington, TX
    Posts
    60

    Re: Excel drop down lists

    Unfortunately, my Excel help file doesn't seem to have any information on VBA....does yours have a code example for validating a List as opposed to Whole Numbers?

  8. #8
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Excel drop down lists

    To validate list you need to change the type to xlValidateList and in Formula one place a comma delimted list...

    You can also specify an operator after the alertstyle..for example
    VB Code:
    1. With Range("A14").Validation
    2.         .Add Type:=xlValidateList, AlertStyle:=xlValidAlertInformation, Formula1:="5,6,7,10"
    3.         .InputTitle = "Integers"
    4.         .ErrorTitle = "Integers"
    5.         .InputMessage = "Enter either 5,6,7 or 10 only"
    6.         .ErrorMessage = "You must enter either 5,6,7 or 10"
    7. End With
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  9. #9

    Thread Starter
    Member
    Join Date
    Apr 2005
    Location
    Arlington, TX
    Posts
    60

    Re: Excel drop down lists

    Run-time error '1004':
    Application-defined or object-defined error

    ...any ideas on what it's yelling at me for?

    *Note* When I hit 'Debug' it highlights the following line:
    VB Code:
    1. .Add Type:=xlValidateList, AlertStyle:=xlValidAlertInformation, Formula1:="5,6,7,10"

  10. #10
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Excel drop down lists

    If its already been added you will need to modify it instead..
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  11. #11

    Thread Starter
    Member
    Join Date
    Apr 2005
    Location
    Arlington, TX
    Posts
    60

    Re: Excel drop down lists

    Ok...one last question (hopefully). Is there a way to check to see if a cell already has validation or not?

  12. #12

    Thread Starter
    Member
    Join Date
    Apr 2005
    Location
    Arlington, TX
    Posts
    60

    Re: Excel drop down lists

    Nevermind...I found a way to fix it. Thanks for all your help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width