Code Or Program A Data Validation List - VBA Code Examples
Có thể bạn quan tâm
In this Article
- Creating a Drop Down List Using VBA
- Populate a Drop Down List From a Named Range in VBA
- Removing the Drop Down List
- VBA Coding Made Easy
This tutorial will demonstrate how to use Data Validation to work with drop-down lists in Excel using VBA.
Excel Data Validation allows you to limit what value(s) may be entered in a cell or range. You can limit entries to positive integers, text, dates, and much more. In this tutorial, we are going to look at how to create a Data Validation Drop-Down List in a cell using VBA.
Note: An alternative to a Data Validation Drop-Down list is a ListBox object. ListBoxes can be added to Excel worksheets. ListBoxes can trigger macros that run every time a ListBox value is changed. ListBoxes are also used in VBA Userforms.
Creating a Drop Down List Using VBA
We have the text Fruit in cell A1, and we are going to create a drop-down list in cell A2, with five entries.

We will use the Validation.Add method and specify that the Type parameter is xlValidateList. You can add the specific items you would like in your list using the Formula1 parameter.
The following code will create a data validation drop-down list in cell A2:
Sub DropDownListinVBA() Range("A2").Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _ Formula1:="Orange,Apple,Mango,Pear,Peach" End SubThe result is:

Populate a Drop Down List From a Named Range in VBA
You can use a named range containing the items, to populate a drop-down list in VBA. We have the named range Animals shown below:

We have to set the Formula1 parameter equal to the named range. The following code will create a data validation drop-down list in cell A7 based on the items in the named range:
Sub PopulateFromANamedRange() Range("A7").Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _ Formula1:="=Animals" End SubThe result is:

Removing the Drop Down List
You can use the Validation.Delete method to remove the drop-down list from the cell. The following code would remove the drop-down list from cell A7 in the example above:
Sub RemoveDropDownList() Range("A7").Validation.Delete End SubVBA Coding Made Easy
Stop searching for VBA code online. Learn more about AutoMacro – A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users!

Learn More!
Từ khóa » Visual Basic Excel Drop Down Menu
-
Data Validation Drop Down List With VBA In Excel (7 Applications)
-
Run A Macro From A Drop Down List [VBA] - Get Digital Help
-
Creating A Drop-down List In Excel Using Tools Or Macros
-
DropDown Method Example - Microsoft Docs
-
Create A Drop-down List - Microsoft Support
-
Excel VBA Drop Down Lists Using Data Validation
-
How To Make Drop Down Lists From A Macro In Excel - YouTube
-
Add A Drop Down Menu To A Cell In Excel - Data Validation Macro
-
Hướng Dẫn Cách Thêm Drop-down List Với Dấu Tick Và Dấu Cross Trong ...
-
How To Get Excel Drop Down List Source In Vba - Stack Overflow
-
Excel Vba Userform Drop Down List From Range - Hỏi Đáp
-
Excel VBA ComboBox Lists - Contextures
-
How Do You Get An Excel Drop Down List Source In VBA? - Quora
-
VBA Code To Select Value From Drop Down List In Excel