Data Validation Drop Down List with VBA in Excel (7 Applications)

Data validation dropdown lists are a very useful feature to perform various Excel related tasks. Implementing VBA is the most effective, quickest, and safest method to run any operation in Excel. In this article, we will show you 7 different applications of the data validation drop-down list in Excel with the VBA macro.


7 Methods with VBA in Data Validation Drop Down List in Excel

In this section, you learn 7 different applications of the data validation drop-down list in Excel with VBA macro.

1. Embed VBA to Create a Data Validation Drop Down List in Excel

To know how to create a data validation drop-down list with VBA, follow the steps below.

Steps:

  • At the beginning, press Alt + F11 on your keyboard or go to the tab Developer -> Visual Basic to open Visual Basic Editor.

  • Then, in the pop-up code window, from the menu bar, click Insert -> Module.

  • After that, copy the following code and paste it into the code window.
Sub CreateDropDownList()
Range("B5").Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:="Grapes, Orange, Guava, Mango, Apple"
End Sub

Your code is now ready to run.

Create excel vba data validation drop down list

This piece of code will create a dropdown list in Cell B5. And the dropdown list will carry values “Grapes, Orange, Guava, Mango, Apple“.

  • Now, press F5 on your keyboard, or from the menu bar select Run -> Run Sub/UserForm. You can also just click on the small Run icon in the sub-menu bar to run the macro.

After the code execution, look at the image below to see the result.

Result of creating excel vba data validation drop down list

As we can see from the above image is that there is a dropdown list created in Cell B5 with the values “Grapes, Orange, Guava, Mango, Apple“.

Read More: VBA to Select Value from Drop Down List in Excel


2. Generating Drop Down List by Named Range with VBA in Excel

When you don’t want to write each and every value of a dropdown list inside the code, then you can put all the names inside a defined name and later use it to call the range of values. This is a very convenient method for creating a dropdown list in Excel.

In this section, you will learn how to generate a dropdown list from a given list using Named Range with VBA code.

Steps:

  • Firstly, select the range where the values of the dropdown list are present (in our case, the range is B5:B9).
  • Secondly, right-click on the selected range.
  • A list of options will appear. From there, select Define Name…

  • After that, a New Name pop-up box will appear. In the Name box, write any name that you like (we named our cell Fruits).
  • Later, click OK.

  • We have successfully named the range B5:B9 Fruits (shown in the picture below).

Now we will use this defined name in our VBA code. The steps to do that are shown below.

  • Same way as before, open Visual Basic Editor from the Developer tab and Insert a Module in the code window.
  • Then, in the code window, copy the following code and paste it.
Sub GenerateDropDownList()
Range("B12").Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:="=Fruits"
End Sub

Your code is now ready to run.

Generating excel vba data validation drop down list from named range

This piece of code will create a dropdown list in Cell B12 with the values “Grapes, Orange, Guava, Mango, Apple” that are defined in the name Fruits.

  • Later, Run the macro. The result is shown in the image below.

Result of generating excel vba data validation drop down list with named range

As a result, we can see from the above image is that there is a dropdown list created in Cell B12 with the values “Grapes, Orange, Guava, Mango, Apple“.

Read More: How to Use Named Range for Data Validation List with VBA in Excel


3. Producing Data Validation Drop Down Box from a Given List with Macro

If you are not very fond of the Named Range option, then this section is for you. Here, you will learn how to produce a dropdown list from a range existing in a dataset.

Steps:

  • As shown before, open Visual Basic Editor from the Developer tab and Insert a Module in the code window.
  • Then, copy the following code and paste it into the code window.
Sub ProduceDropDownList()
With Range("B12").Validation
 .Add xlValidateList, xlValidAlertStop, xlBetween, "=$B$5:$B$10"
 .InCellDropdown = True
End With
End Sub

Your code is now ready to run.

Producing excel vba data validation drop down list

This piece of code will produce a dropdown list in Cell B12 with the values present in the range B5:B9.

  • Now, Run the macro and look at the following image to see the output.

Result of producing excel vba data validation drop down list

As a result, we can see from the above image that there is a dropdown list created in Cell B12 with the values “Grapes, Orange, Guava, Mango, Apple” that we stored in Cell B5 to B9 in the worksheet.


4. Implementing VBA to Create Multiple Drop Down Lists in Excel

You can create multiple dropdown lists with VBA macro. Let’s see how to do that in Excel.

Steps:

  • At first, open Visual Basic Editor from the Developer tab and Insert a Module in the code window.
  • Secondly, in the code window, copy the following code and paste it.
Sub MultipleDropDownList(iTarget As Range, iSource As Range)
    'to delete and add validation in the target range
    With iTarget.Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
        xlBetween, Formula1:="=" & iSource.Address
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = ""
        .ErrorTitle = ""
        .InputMessage = ""
        .ErrorMessage = ""
        .ShowInput = True
        .ShowError = True
    End With
End Sub

Sub DropDownRange()
    MultipleDropDownList Sheet7.Range("B5:B10"), Sheet7.Range("A1:A3")
End Sub

Your code is now ready to run.

Creating multiple data validation drop down list in Excel with VBA

This code will generate a dropdown list in every cell from range B5 to B9.

  • Thirdly, Run the macro. Look at the gif below to see the result.

Result of creating multiple excel vba data validation drop down list

Each and every cell from range B5 to B9 are now holding a dropdown list.


5. Applying VBA to Create Drop-Down List with User-Defined Function

You can also create a dropdown list with a user-defined function (UDF) in Excel.

Steps to get that are shown below.

Steps:

  • At the beginning, right-click on the sheet where you want to implement the UDF to create a dropdown list.
  • Then, select View Code from the appeared list. As shown below, we right-clicked on the sheet named UDF where our dataset is stored, and selected View Code from the options.

  • Then, copy the following code and paste it into the auto-generated code window.
Public Function DropDownUDF(iSource As Range) As Variant
    'to delete and add validation in the specified range
    With Selection.Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
        xlBetween, Formula1:="=" & iSource.Address
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = ""
        .ErrorTitle = ""
        .InputMessage = ""
        .ErrorMessage = ""
        .ShowInput = True
        .ShowError = True
    End With
    'this will return the first value
    'this reset the values when formula in sheet are refreshed
    DropDownUDF = VBA.Val(iSource(1))
End Function
  • Don’t run this code. Save it.

UDF for excel vba data validation drop down list

  • Then, go back to the worksheet of interest.
  • Following that, pick any cell where you want to create the dropdown list (in our case, it is Cell B11).
  • In that cell, write the newly created function – DropDownUDF – the way you write any other function. Meaning, that first put an equal sign (=), then write the function name, DropDownUDF and pass the cell references (B5:B9) inside the parentheses.

The formula in Cell B11 will be like this:

=DropDownUDF(B5:B9)

Applying UDF for excel vba data validation drop down list

  • Then, press Enter.

Result of UDF for excel vba data validation drop down list

As a result, you will get a dropdown list created by UDF in Cell B11 with the values “Grapes, Orange, Guava, Mango, Apple” that are stored in range B5:B9 and which we passed inside the function.


6. Extracting Data from Different Sheet in a Drop Down List with VBA

Look at the following image. We have a dataset in the sheet named List.

What we are going to do here is, we will create a dropdown list in Cell B5 of the sheet named Target (shown in the picture below). The values of that dropdown list will be the values from the range B5:B9 of the List sheet.

Let’s see the steps on how to do that with VBA.

Steps:

  • Firstly, open Visual Basic Editor from the Developer tab and Insert a Module in the code window.
  • Then, copy the following code and paste it into the code window.
Private Sub DropDownFromSheet()
'to store the dropdown list in cell B5
'you can replace "B5" with any other cell
With Range("B5").Validation
.Delete
'to extract data from "List" sheet and "B5:B9" range
'you can replace "=List!B5:B9" with your sheet name and range
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
Operator:=xlBetween, Formula1:="=List!B5:B9"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
End Sub

Your code is now ready to run.

Extracting data from different sheet for excel vba data validation drop down list

  • Later, Run the macro and look at the following image to see the output.

Result of Extracting data from different sheet for excel vba data validation drop down list

As a result of the successful code execution, there is a dropdown list created in Cell B5 of the Target worksheet with the values “Grapes, Orange, Guava, Mango, Apple” that are stored in range B5:B9 of the List spreadsheet.

Read More: How to Make a Dynamic Data Validation List Using VBA in Excel


7. Deleting Data Validation Drop Down List from Excel with VBA Macro

This section will show you how to delete a dropdown list from Excel. We will show you how to delete the existing dropdown list in Cell B5 (shown in the image below) with the VBA macro.

The steps to execute this are given below.

Steps:

  • At first, open Visual Basic Editor from the Developer tab and Insert a Module in the code window.
  • Then, in the code window, copy the following code and paste
Sub DeleteDropDownList()
Range("B5").Validation.Delete
End Sub

Your code is now ready to run.

  • Later, Run the macro and look at the following image.

As you can see from the above image, there is no dropdown list anymore in Cell B5. Finally, we have learned how to delete an existing dropdown list from a spreadsheet with VBA.


Conclusion

To conclude, this article showed you 7 different applications of the data validation drop-down list in Excel with the VBA macro. I hope this article has been very beneficial to you. Feel free to ask any questions regarding the topic.


Related Articles

Get FREE Advanced Excel Exercises with Solutions!
Sanjida Ahmed
Sanjida Ahmed

Sanjida Ahmed, who graduated from Daffodil International University with a degree in Software Engineering, has worked with SOFTEKO since 2021. She has written over 100 articles on Excel & VBA and, since 2022, has worked as the Project Manager of the Excel Extension Development Project in the Software Development Department. Since starting this software development, she has established an outstanding workflow encompassing a full SDLC. She always tries to create a bridge between her skills and interests in... Read Full Bio

2 Comments
  1. How to I integrate these 2 codes so I can use drop down lists to enter new data from the top of a table and once all data is entered have the new data move down the table so I can keep entering data from the top? This would be used in a row with 5 active columns to which only 3 will have coded drop down menus and the other 2 have manually input data.

    Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    On Error GoTo enditall
    Application.EnableEvents = False
    If Target.Cells.Column = 4 Then
    If Target.Cells.Row = 2 Then
    N = Target.Cells.Row
    If Range(“D” & N).Value “” Then
    Range(“D2”).EntireRow.Insert
    Range(“A2”).Select
    End If
    End If
    End If

    enditall:
    Application.EnableEvents = True
    End Sub

    Sub CreateDropDownList()
    Range(“B5″).Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:=”Grapes, Orange, Guava, Mango, Apple”
    End Sub

    • Hello, MAC!
      Thanks for sharing your problem with us!
      To integrate these 2 codes, all you need to do is just define the first sub-procedure name in the second part of the code and add the sheet name there before the range-bounded combination. “Worksheet_Change Sheet1.Range(“B5”).Validation…….” like this.

      The code should look like this.

      Private Sub Worksheet_Change(ByVal Target As Excel.Range)
      On Error GoTo enditall
      Application.EnableEvents = False
      If Target.Cells.Column = 4 Then
      If Target.Cells.Row = 2 Then
      N = Target.Cells.Row
      If Range("D" & N).Value "" Then
      Range("D2").EntireRow.Insert
      Range("A2").Select
      End If
      End If
      End If
      
      enditall:
      Application.EnableEvents = True
      End Sub
      
      Sub CreateDropDownList()
      Worksheet_Change Sheet1.Range("B5").Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:=”Grapes, Orange, Guava, Mango, Apple”

      Hope this will help you!
      Good Luck!

      Regards,
      Sabrina Ayon
      Author, ExcelDemy
      .

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo