Excel VBA : Filter data based on drop down selection

Suppose you are asked to add a combo box (drop down) in your sheet and pull values from a range based on selection in the combo box (drop down).

How to add a combo box (drop down)

  1. Check if Developer tab is enabled.

If it is not enabled, you need to add Developer Tab. To add Developer tab into Excel 2010 and 2013, follow the instructions below:
  1. Click the File tab
  2. Click the Options tab
  3. Click the Customize Ribbon at the left
  4. At the right, select the Main Tabs from Customize The Ribbon drop down box
  5. Check the Developer item
  6. Click the OK button.

2.  On the Developer tab, in the Controls group, click Insert, and then under Form Controls, click Combo box Button.

3. Click on the cell(s) where you want the drop down to appear.

4. To display the text box, drag the left-center sizing handle to the right.

Create dummy data

Enter data into column A to C.

Task : Filter data based on column A values listed in drop down.
  1. Right click on the drop down and select Format Control and then go to Control tab
  2. In the Input range:  enter A2:A6 (values to display in the drop-down list).
  3. In the Cell link:, enter D1  (cell reference that tells you which value is selected in the drop-down list).
  4. Press ALT + F11 to open VBA editor window.
  5. Click on Insert Tab and then click Module option
  6. In the module, paste the following code
Sub FilterData()

'Get the Name of ComboBox (Drop Down)
Cbo = Application.Caller

'Get the line number of the entry and the entry data
With Sheets("Sheet1").Shapes(Cbo).ControlFormat
CboLine = .ListIndex
cboData = .List(CboLine)
End With

' turn off any autofilters that are already set
Sheets("Sheet1").AutoFilterMode = False

 ' filter column A based on combo box selection
 ActiveSheet.UsedRange.AutoFilter 1, cboData

  ' copy the visible cells to cell E8
ActiveSheet.UsedRange.SpecialCells(xlCellTypeVisible).Copy Range("E8")

' Turn off any autofilters that are already set
Sheets("Sheet1").AutoFilterMode = False

End Sub

Next Step : Right Click on the drop down and click Assign Macro and select FilterData and then click OK.

Customize the above code

1. Copy visible cells from one column of a filtered range
ActiveSheet.UsedRange.AutoFilter 1, "Sandy"

2. Customize filter range - Filter only two columns
lastRow = Sheets("Sheet1").Range("A" & Sheets("Sheet1").Rows.Count).End(xlUp).Row

Set filterRange = Sheets("Sheet1").Range("A1:B" & lastRow)

' In this case we are filtering Deepanshu from column A
filterRange.AutoFilter 1, "Deepanshu"

3. Copy data only when at least one record is available after applying filter
Sheets("Sheet1").UsedRange.AutoFilter 1, cboData

If Nrows < 2 Then
MsgBox ("No record available")
' Your code

4. Copy data from second row (Excluding header) 
Related Link - Filtering and Pasting Data to New Sheet

Excel Tutorials : 100 Excel Tutorials

About Author:

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 7 years of experience in data science and predictive modeling. During his tenure, he has worked with global clients in various domains like banking, Telecom, HR and Health Insurance.

While I love having friends who agree, I only learn from those who don't.

Let's Get Connected: Email | LinkedIn

Get Free Email Updates :
*Please confirm your email address by clicking on the link sent to your Email*
Related Posts:
5 Responses to "Excel VBA : Filter data based on drop down selection"
  1. Great! Had been looking for something like this all over the internet...

  2. Hi, great to see your website. I like the content and the research done behind every aspect of your blog. It looks great and very knowledgeable. Keep it up the good work.
    CWNA-107 exam Question


  3. Thank you so much for sharing this worth able content with us. The concept taken here will be useful for my future programs and i will surely implement them in my study. Keep blogging article like this.

    Ms excel training in chennai

  4. Nice post By reading your blog, i get inspired and this provides some useful information. Thank you for posting this exclusive post for our vision.
    Digital Marketing Training


Next → ← Prev