How to add Data validation drop down list in Excel VBA?
Two different methods to create drop down list are explained here - but all are related to validation list. Note: Combo-box control that also display list of values in dropdown is not explained in this page.- Adding Drop Down to Range of Cell
- Create User defined function
- To add dropdown at any location using formula
1. [Vba] Add Drop Down Validation list to Range
For example, lets assume you would like to add validation list to cell range D1:D100. This below sub procedure can be used for this purpose.Sub DropDownToRange(rngTarget As Range, rngSource As Range)
'Delete & Add Validation in Target Range
With rngTarget.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=" & rngSource.Address
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
End Sub
The above procedure takes target cells range & validation list for drop down source range as two parameters. You can call the above function as given in this test function.
Sub TestDropDown()
DropDownToRange Sheets(1).Range("D1:D100"), Sheets(1).Range("A1:A3")
End Sub
Once you run this above test code, it will add drop down list in cells from D1 to D100.
2.Formula to Add Dynamic Drop Down Validation List [VBA-UDF]
This step is about creating a formula & then adding drop down list in any cell using the formula. Note: Don’t execute this code with F5. Go to any sheet. Follow the steps explained below the code.Public Function DropDownInLine(rngSource As Range) As Variant
'Delete & Add Validation in Selected Cell
With Selection.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=" & rngSource.Address
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
'Return first value
'This is just an illustration - It is better to omit this line.
'If formula in sheet are refreshed, this reset the values
DropDownInLine = VBA.Val(rngSource(1))
End Function
The above code should be added to a module in VBA project. Just follow these steps to create a dynamic list in your Excel.
- Press Alt + F11 in Excel to view VB Editor.
- Click on Insert Menu & choose Module.
- This will insert a new module in current project.
- Copy paste the above code to this module.
Add Values to Drop down list using a variable
In all the code snippets above, drop down list source values are taken from range. What if you need to add list from a variable string? Just change the AddrRange.Range with a string variable as in below example. ListValue = "Value1, Value2, Value3, Value4"
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=" & ListValues
Isn’t this so easy than to mention the range address? It is easy to create this list dynamically from within the Excel VBA code.