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.
Printable View
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.
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:
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)
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.
Yes you can in the same format as above...
VB Code:
ActiveCell.Validation
Extract from Excel VBA Help
Quote:
Using the Validation Object
Use the Validation property to return the Validation object. The following example changes the data validation for cell E5.
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:
Range("e5").Validation _ .Modify xlValidateList, xlValidAlertStop, "=$A$1:$A$10"
VB Code:
With Range("e5").Validation .Add Type:=xlValidateWholeNumber, _ AlertStyle:=xlValidAlertInformation, _ Minimum:="5", Maximum:="10" .InputTitle = "Integers" .ErrorTitle = "Integers" .InputMessage = "Enter an integer from five to ten" .ErrorMessage = "You must enter a number from five to ten" 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....VB Code:
With Range("e5").Validation .Add Type:=xlValidateWholeNumber, _ AlertStyle:=xlValidAlertInformation, _ Minimum:="5", Maximum:="10" .InputTitle = "Integers" .ErrorTitle = "Integers" .InputMessage = "Enter an integer from five to ten" .ErrorMessage = "You must enter a number from five to ten" End With
I'll try but this was taken from the VBA help file in Excel..
VB Code:
'The with object allows you to use a object and it's properties by pressing . With Range("e5").Validation 'we are adding a validation rule to the spreadsheet at one cell.. .Add Type:=xlValidateWholeNumber, _ 'Specifies the whole cell content to check AlertStyle:=xlValidAlertInformation, _ 'this is the alert style that will be shown Formula1:="5", Formula2:="10" 'This is the range of values that are valid 'The next four objects are the message settings to be returned to the user .InputTitle = "Integers" .ErrorTitle = "Integers" .InputMessage = "Enter an integer from five to ten" .ErrorMessage = "You must enter a number from five to ten" 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..
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?
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:
With Range("A14").Validation .Add Type:=xlValidateList, AlertStyle:=xlValidAlertInformation, Formula1:="5,6,7,10" .InputTitle = "Integers" .ErrorTitle = "Integers" .InputMessage = "Enter either 5,6,7 or 10 only" .ErrorMessage = "You must enter either 5,6,7 or 10" End With
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:
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertInformation, Formula1:="5,6,7,10"
If its already been added you will need to modify it instead..
Ok...one last question (hopefully). Is there a way to check to see if a cell already has validation or not?
Nevermind...I found a way to fix it. Thanks for all your help.