I want to create a drop down with some fixed values in excel, Most of the examples are about input from sheets to drop down. My case I want to put "IF, AND, OR" inside the drop can anyone suggest how to achieve this in excel? I found this example for existing values of sheet every place.
答案 0 :(得分:1)
You don't need VBA to do that, but you can do it in code. The easiest way for a set list is go to the Data tab, then to Data Validation. Then, under Settings choose list as your validation criteria and enter your three values(comma separated). You can copy this to any cell by copying and pasting formatting or left-click dragging and copying formatting.
If you really want to do it in VBA
Using the array
Sub CreateDropdownList()
' replace "A5:A12" with your named range if you have one
Range("A5:A12").Select ' range where you've listed your choices
' now sort them alphabetically, replace sheet1 with your sheetname
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("A5"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortTextAsNumbers
With ActiveWorkbook.Worksheets("Sheet1").Sort
' you can use your named range here as well
.SetRange Range("A5:A12")
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ActiveWorkbook.Names.Add Name:="choices", RefersToR1C1:= _
"=Sheet2!R5C1:R12C1"
Range("G13").Select 'this is the cell you want the dropdown in
With Selection.Validation
.Delete
'without array
'.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
'xlBetween, Formula1:="IF, AND, OR"
'with array
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=choices"
.IgnoreBlank = True
.InCellDropdown = True
.InputMessage = "Select a value"
.ErrorMessage = "No value selected"
.ShowInput = True
.ShowError = True
End With
End Sub
答案 1 :(得分:0)
Not entirely clear what you want, but I'm guessing maybe you want dependent drop downs based on the value of another cell? See more information about this here, you can make a sheet with all the potential drop down values and then use =INDIRECT(SUBSTITUTE(CellName," ","")) in the data validation tool. So, if you wanted the dependent value to be in Cell B1, and the drop down in B2, you would put the data validation (data tab) and drop down box in B2 (select "list), with the source being =INDIRECT(SUBSTITUTE(B1," ","")). The link will probably help explain it a bit better with the pictures.
In the example above, you use the name manager (formula tab) to name all of the potential lists you want, without spaces (so if you had, a list called "New Employees", in the name manager you'd call it NewEmployees. Then, on your sheet with all the values in, you have the heading as the list title (so New Employees), with all the values following it that you only want to display if new employees is the value of the dependent cell.