Author: Oscar Cronquist Article last updated on May 29, 2019

This article demonstrates how to add or remove a value in a regular drop down list based on a list located on a worksheet using VBA.

Cell B3 contains a regular drop down list which you will find on tab "Data" on the ribbon. The data source used in the drop down list is from column H. I am using an Excel defined Table that grows when new values are added.

Enter a value in cell E3 and press the "Add" button to add the value to the list in column H. You can also remove a value using the "Remove" button, note that this is case-sensitive.

The animated gif below demonstrates how to add or remove values from the list.

What you will learn in this article

  • How to insert a regular drop down list.
  • How to use an Excel Table as a data source for a drop down list.
  • How to convert a data set to an Excel defined Table.
  • How to add buttons to a worksheet.
  • How to change button text.
  • How to create a macro that adds a value to a list
  • Name a macro
  • Find last non-empty value in list
  • Copy entered value to first empty cell in list
  • How to create a macro that deletes a specific value in a list
  • Iterate through a given column in an Excel defined Table
  • Find a value in an Excel defined Table column
  • Delete a cell in a list
  • How to assign a macro to a button.
  • Where the macros are located.

How to build a drop down list

  1. Select the cell you want to use.
  2. Go to tab "Data" on the ribbon.
  3. Press with mouse on the "Data Validation" button.
  4. Select "List".
  5. Excel won't let you type a reference to an Excel Table, however, there is a workaround. See below.

How to use an Excel defined Table as a data source for a drop down list

Use the following formula in order to be able to reference an Excel Table:

=INDIRECT("Table1[List]")

How to create an Excel defined Table

  1. Select any cell in the data set you want to convert.
  2. Go to tab "Insert" on the ribbon.
  3. Press with left mouse button on "Table" button.
  4. Press with left mouse button on checkbox "My Table has headers".
  5. Press with left mouse button on OK button.

How to add a value to a list using VBA

'Name macro
Sub AddValue()

'Declare variable and data type
Dim i As Single

'Save row number of the first empty cell in column H to variable i
i = Worksheets("Sheet1").Range("H" & Rows.Count).End(xlUp).Row + 1

'Save value entered in cell E3 to first empty cell in column H.
Worksheets("Sheet1").Range("H" & i) = Worksheets("Sheet1").Range("E3")

'Clear cell E3
Worksheets("Sheet1").Range("E3") = ""

End Sub

How to delete a specific value in a list using VBA

'Name macro
Sub RemoveValue()

'Dimension variables and declare data types
Dim i As Single
Dim Cell As Range

'Save row number of last non-empty cell in column H to variable i
i = Worksheets("Sheet1").Range("H" & Rows.Count).End(xlUp).Row

'Assign the first non-empty cell to object Cell
Set Cell = Worksheets("Sheet1").Range("H" & i)

'Repeat everything between Do and Loop until row of object Cell is equal to 2 
Do Until Cell.Row = 2

    'Check if object cell is equal to value in cell E3 and is not empty
    If Worksheets("Sheet1").Range("E3") = Cell And Cell <> "" Then

        'Delete cell 
        Cell.Delete Shift:=xlUp

        'Clear cell E3
        Worksheets("Sheet1").Range("E3") = ""

        'Stop macro
        Exit Sub
    End If

    'Save object reference 1 row below current object to variable Cell
    Set Cell = Cell.Offset(-1, 0)
Loop

'Show a message box telling the user that the value cant be found in the list
MsgBox "CanĀ“t find value: " & Worksheets("Sheet1").Range("E3")

End Sub

Where to put the VBA code?

  1. Press Alt + F11
  2. Press with left mouse button on "Insert" on the top menu.
  3. Press with left mouse button on "Module" to add a code module to your workbook.
  4. Paste macro to code module.
Note, save your workbook with file extension *.xlsm (macro-enabled) in order to attach the code to the workbook.

Insert button and assign a macro

  1. Go to tab "Developer" on the ribbon.
  2. Press with left mouse button on "Insert" button.
  3. Select "Button" (Form Control).
  4. Press and hold with left mouse button on worksheet.
  5. Drag and release the mouse button to create the button.
  6. A dialog box appears, select the macro you want to assign to the button.
  7. Press with left mouse button on OK button.

Change button text

  1. Press with right mouse button on on a button.
  2. A context menu appears, press with left mouse button on "Assign Macro...".
  3. A dialog box shows up, select the macro you want to assign.
  4. Press with left mouse button on OK button.

The macro starts every time the user press with left mouse button ons on the button. Create a new button and assign the second macro.

Move the buttons below cell E3.