Vlookup Function That Searches The Entire Workbook - UDF

Add to Favorites
This UDF is a Vlookup function that searches the entire workbook in Excel. The syntax and usage of the function is exactly the same as the regular Vlookup function in Excel. The only difference is that this function will search through all of the worksheets in the workbook and return the associated value of the first match that it finds. This Vlookup function only searches the table_array (specified in the function by the user) for the specified values on each worksheet in the workbook in Excel. It does not search the entire worksheet. The function searches the table_array on the first worksheet and, if no match is found, the function moves to the second worksheet and searches the same table_array on that sheet. This cycle continues through all of the worksheets in the active workbook until Excel has found a match or finished searching all of the worksheets in the workbook.

Where to install the macro:  Module

UDF to Perform a Vlookup That Searches The Entire Workbook - All Worksheets in The Workbook

Function VLOOKUPWORKBOOK(Look_Value As Variant, Tble_Array As Range, Col_num As Integer, Optional Range_look As Boolean)
'Vlookup function that will search all worksheets in the workbook - however, the data table that is being searched must be in the same
'location on every worksheet.

Dim wSheet As Worksheet
Dim vFound

On Error Resume Next

For Each wSheet In ActiveWorkbook.Worksheets

With wSheet

Set Tble_Array = .Range(Tble_Array.Address)

vFound = WorksheetFunction.VLookup(Look_Value, Tble_Array, Col_num, Range_look)

End With

If Not IsEmpty(vFound) Then Exit For

Next wSheet

Set Tble_Array = Nothing

VLOOKUPWORKBOOK = vFound

End Function










How to Install the Macro
  1. Select and copy the text from within the grey box above.

  2. Open the Microsoft Excel file in which you would like the Macro to function.

  3. Press "Alt + F11" - This will open the Visual Basic Editor - Works for all Excel Versions.  Or For other ways to get there, Click Here.

  4. 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.

  5. If the Macro goes in a Module, Click Here, otherwise continue to Step 8.

  6. If the Macro goes in the Workbook or ThisWorkbook, Click Here, otherwise continue to Step 8.

  7. If the Macro goes in the Worksheet Code, Click Here, otherwise continue to Step 8.

  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.

  9. You are now ready to run the macro.


Excel Forum