CustomSearch
is a custom LAMBDA function for Excel that enables advanced search operations within a table. This function searches for text within a specified column and returns specified columns of the matching rows.
=LAMBDA(SearchText; DataTable; SearchColumn; ReturnColumns; NotFoundText; ErrorText;
LET(
ColumnExists; ISNUMBER(MATCH(SearchColumn; SEQUENCE(1; COLUMNS(DataTable)); 0));
ReturnColumnsValid; AND(ISNUMBER(MATCH(ReturnColumns; SEQUENCE(1; COLUMNS(DataTable)); 0)));
IF(
ISBLANK(SearchText);
NotFoundText;
IF(
NOT(ColumnExists);
ErrorText;
IF(
NOT(ReturnColumnsValid);
ErrorText;
LET(
FilteredData; IFERROR(FILTER(DataTable; ISNUMBER(SEARCH(UPPER(SearchText); UPPER(INDEX(DataTable; SEQUENCE(1; ROWS(DataTable)); SearchColumn))))); "");
IF(
COUNTA(FilteredData) = 0;
NotFoundText;
LET(
Result; IFERROR(CHOOSECOLS(FilteredData; ReturnColumns); NotFoundText);
IF(
ISERROR(Result);
ErrorText;
Result
)
)
)
)
)
)
)
)
)
- SearchText1: The text or value that you want to search for in the DataTable.
- DataTable: The data table to be searched.
- SearchColumn: The column number in the DataTable where the search will be performed.
- ReturnColumns: The column numbers in the DataTable from which the data should be returned.
- NotFoundText: The text that should be returned if no match is found.
- ErrorText: The text that should be returned if there is an error in the search parameters or execution.
Syntax
=CustomSearch(String/Num; Range/Array; Num; Num_Array; String; String)
- Open Excel and go to the
Formulas
tab. - Click on
Name Manager
and then clickNew
. - In the
Name
field, enterCustomSearch
. - In the
Refers to
field, paste the LAMBDA function definition provided above. - Click
OK
to save the new named formula.
To use the CustomSearch
function in your Excel workbook, follow these steps:
-
Prepare your data table:
- Ensure your data table is defined and named (e.g.,
Table1
).
- Ensure your data table is defined and named (e.g.,
-
Specify the search text:
- Enter the search text in a cell, for example, cell
A1
.
- Enter the search text in a cell, for example, cell
-
Enter the formula:
- Use the following formula to perform the search and return the results:
=CustomSearch(A1, Table1, 2, {1, 2, 3, 4}, "No data found", "Error")
Assume you have the following data in Table1
:
ID | Name | Department | Salary |
---|---|---|---|
1 | Alice | HR | 50000 |
2 | Bob | IT | 60000 |
3 | Charlie | Finance | 70000 |
4 | David | IT | 65000 |
You want to search for the term in cell A1
within the Name
column (second column) and return the ID
, Name
, Department
, and Salary
columns.
- Enter
Bob
in cellA1
. - Use the formula:
=CustomSearch(A1, Table1, 2, {1, 2, 3, 4}, "No data found", "Error")
- The result will be:
ID | Name | Department | Salary |
---|---|---|---|
2 | Bob | IT | 60000 |
- Ensure that
SearchColumn
andReturnColumns
are within the bounds of theDataTable
columns. SearchText
is case-insensitive.- If no matching data is found, the function returns
NotFoundText
. - If there is an error in the parameters, the function returns
ErrorText
.
The CustomSearch
LAMBDA function provides a powerful and flexible way to search within a data table and retrieve specific columns. By following the steps outlined in this guide, you can easily implement and use this function in your Excel workbooks.
If you find any issues or have suggestions for improvements, please open an issue or submit a pull request on the GitHub repository.
This project is licensed under the MIT License.