Premium Excel Course Now Available!
Build Professional - Unbreakable - Forms in Excel
45 Tutorials - 5+ Hours - Downloadable Excel Files
Instant Access! - Lifetime Access!
Vlookup Macro to Return All Matching Results from a Sheet in Excel
Add to Favorites
Favorited
This Excel Macro works like a better Vlookup function because it returns ALL of the matching results.
Run the macro and every result that matches your lookup value will be returned in a list, with each value in its own row. (below the macro, I will explain how to change it to work for your data)
Sub Return_Results_Sheet()
'cell that contains the value for which you are searching
searchValue = Range("A2")
'number of the column you will search through to find the searchValue
searchCol = 6
'number of the column that contains the data you want to return
'the data is returned from the same row that contains a value that matches the searchValue
returnValueCol = 7
'the column where you want to store the returned results
outputValueCol = 2
'the row in which you want to start the list of returned results
'everything from this row down must be empty!
outputValueRowStart = 2
'find the last row so don't have to search every cell
lastRow = Cells(Rows.Count, searchCol).End(xlUp).Row
'clear the results display area
Range(Cells(outputValueRowStart, outputValueCol), Cells(Rows.Count, outputValueCol)).Clear
'search for the content and return it if a match is found
'use a for loop here becauase this is a simple check
For i = 1 To lastRow
'get the value that is cheched against the searchValue
checkValue = Cells(i, searchCol).Value
'if a match is found, do everything else
If checkValue = searchValue Then
'get the value we will need to return
returnvalue = Cells(i, returnValueCol)
'get last row for the output column
nextOutputRow = Cells(Rows.Count, outputValueCol).End(xlUp).Row + 1
'Make sure the nextOutputRow is >= the outputValueRowStart variable
' or then set the nextOutputRow to the value of the outputValueRowStart
If nextOutputRow < outputValueRowStart Then
nextOutputRow = outputValueRowStart
End If
'output the value to the correct cell
Cells(nextOutputRow, outputValueCol).Value = returnvalue
End If
Next i
End Sub
This macro assumes that the source data, through which you will search, is located on the same worksheet.
To make this macro work for you, you will need to change the cell, row, and column references that tell the macro where everything is located. The macro itself has a lot of comments that should explain everything, but I'll go through it below as well.
Change searchValue to the cell reference that contains the lookup value, which is just the value for which you are searching. To do this, change A2 to the necessary cell reference.
Change searchCol to the number of the column that contains the values through which you will search.
Change returnValueCol to the number of the column that contains the actual data that you want to return. This data will be located on the same row as the matching value that id being searched for.
Change outputValueCol to the number of the column where you would like the list of results to be displayed. Make sure there is no data below where these results will display.
Change outputValueRowStart to the number of the row where the results will start to be displayed. This is in case you want to use a header above the displayed results or want to display them lower on the worksheet than the first row.
Notice that there are number for column references instead of letters. This is intentional and has to do with how the code is written. It is very easy to get column numbers, just count starting with A as 1, B as 2, etc. If it is a column really far to the right, just insert the function =COLUMN() into any cell in the column and then hit the enter key and it will give you the number for that column.
I hope this helps! :)
Excel VBA Course - From Beginner to Expert
200+ Video Lessons
50+ Hours of Instruction
200+ Excel Guides
Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)
View Course
()
Vlookup to Return All Matching Results
Tutorial:
Here is an Excel formula that will act like a Vlookup that returns every matching result ...
Remove All Data Validation from a Cell in Excel
Macro: Remove all data validation from a cell in Excel with this free Excel macro. This is a grea...
Vlookup Macro to Return All Matching Results and Stack them with Previous Results
Macro: This is very similar to the other Vlookup type Macro in that it returns all of the results...
Get the First Word from a Cell in Excel
Tutorial: How to use a formula to get the first word from a cell in Excel. This works for a single c...
Extract the Last Word from a Cell in Excel - User Defined Delimiter Text Extraction - UDF
Macro: This UDF (user defined function) extracts the last word or characters from a cell in Excel...
Extract the First Word from a Cell in Excel - User Defined Delimiter Text Extraction - UDF
Macro: This free Excel UDF (user defined function) returns the first word from a cell in Exce...
How to Install the Macro
- Select and copy the text from within the grey box above.
- Open the Microsoft Excel file in which you would like the Macro to function.
- Press "Alt + F11" - This will open the Visual Basic Editor - Works for all Excel Versions.
Or For other ways to get there, Click Here.
For Excel Versions Prior to Excel 2007
Go to Tools > Macros > Visual Basic Editor
For Excel 2007
Go to Office Button > Excel Options > Popular > Click Show Developer tab in the Ribbon. Then go to the Developer tab on the ribbon menu and on the far left Click Visual Basic
- On the new window that opens up, go to the left side where the vertical pane is located. Locate your Excel file; it will be called VBAProject (YOUR FILE'S NAME HERE) and click this.
- If the Macro goes in a Module, Click Here, otherwise continue to Step 8.
- Go to the menu at the top of the window and click Insert > Module
- Another window should have opened within the Visual Basic Editor's window. Within this new window, paste the macro code. Make sure to paste the code underneath the last line of anything else that is in the window.
- Go to Step 8.
- If the Macro goes in the Workbook or ThisWorkbook, Click Here, otherwise continue to Step 8.
- Directly underneath your excel file called VBAProject(your file's name here), click the Microsoft Excel Objects folder icon to open that drop-down list.
- Then, at the bottom of the list that appears, double-click the ThisWorkbook text.
- A new window inside the Visual Basic Editor's window will appear. In this new window, paste the code for the macro. Make sure to paste this code underneath the last line of any other code which is already in the window.
- Go to Step 8.
- If the Macro goes in the Worksheet Code, Click Here, otherwise continue to Step 8.
- Directly underneath your excel file called VBAProject(your file's name here), click the Microsoft Excel Objects folder icon to open that drop-down list.
- Within the list that appears you will see every worksheet that is in that excel file. They will be listed as such: Sheet1(NAME OF SHEET HERE) and under that will be Sheet2(NAME OF SHEET HERE). Select the sheet in which you want the macro to run and double-click that sheet.
- A new window inside the Visual Basic Editor's window will appear. In this new window, paste the code for the macro. Make sure to paste this code underneath the last line of any other code which is already in the window.
- Repeat steps b and c for every sheet you want the macro to work in. Putting the macro in one sheet will not enable it for any other sheets in the workbook.
- Go to Step 8.
- Close the Microsoft Visual Basic Editor window and save the Excel file. When you close the Visual Basic Editor window, the regular Excel window will not close.
- You are now ready to run the macro.