|
-
Aug 15th, 2005, 08:22 AM
#1
Thread Starter
Member
[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.
-
Aug 15th, 2005, 08:48 AM
#2
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
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
-
Aug 15th, 2005, 08:54 AM
#3
Thread Starter
Member
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.
-
Aug 15th, 2005, 09:04 AM
#4
Re: Excel drop down lists
Yes you can in the same format as above...
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:
Range("e5").Validation _
.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:
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
Danny
Never Think Impossible
If you find my answer helpful then please add to my reputation
-
Aug 15th, 2005, 11:15 AM
#5
Thread Starter
Member
Re: Excel drop down lists
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....
-
Aug 15th, 2005, 11:32 AM
#6
Re: Excel drop down lists
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..
Danny
Never Think Impossible
If you find my answer helpful then please add to my reputation
-
Aug 15th, 2005, 11:50 AM
#7
Thread Starter
Member
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?
-
Aug 15th, 2005, 12:25 PM
#8
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:
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
Danny
Never Think Impossible
If you find my answer helpful then please add to my reputation
-
Aug 15th, 2005, 12:37 PM
#9
Thread Starter
Member
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:
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertInformation, Formula1:="5,6,7,10"
-
Aug 15th, 2005, 03:33 PM
#10
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
-
Aug 16th, 2005, 07:54 AM
#11
Thread Starter
Member
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?
-
Aug 16th, 2005, 09:23 AM
#12
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|