Vlookup Macro to Return All Matching Results from a Sheet in Excel
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! :)
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.
- 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.
- If the Macro goes in the Workbook or ThisWorkbook, Click Here, otherwise continue to Step 8.
- If the Macro goes in the Worksheet Code, Click Here, otherwise continue 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.