How to Create a Search Function in Excel: A Comprehensive Guide

Published: October 17, 2024 - 8 min read

Hannah Recker
search function excel

Are you struggling to find specific data in your large Excel spreadsheets? A well-implemented search function can save you hours of manual scanning and dramatically improve your productivity. This guide will walk you through creating effective search capabilities in Excel, from basic built-in functions to advanced VBA solutions.

Understanding Excel’s Built-in Search Functions

Before diving into complex search implementations, it’s essential to grasp Excel’s native search functions: SEARCH and FIND.

The SEARCH Function

The SEARCH function locates a specific text string within another text string and returns the starting position of the found text.

Syntax:

SEARCH(find_text, within_text, [start_num])

ParameterDescription
find_textThe text you want to find
within_textThe text you want to search within
start_num(Optional) The position to start the search from

Example:

=SEARCH(“apple”, “I love apple pie”)

This formula returns 8, as “apple” starts at the 8th character in the string.

 entering a search for Apple, I love Apple pie.

Key differences between SEARCH and FIND:

  1. SEARCH is case-insensitive
  2. SEARCH allows wildcard characters (* and ?)

The FIND Function

The FIND function is similar to SEARCH but with some crucial differences.

Syntax:

FIND(find_text, within_text, [start_num])

The parameters are the same as SEARCH, but FIND is case-sensitive and doesn’t allow wildcards.

Example:

=FIND(“Apple”, “I love apple pie”)

This formula returns an error because “Apple” (capitalized) isn’t found in the string.

Using the find formula and returning an unknown value.

Combining Search Functions with Other Excel Features

To extract text based on search results, combine SEARCH or FIND with the MID function:

=MID(A1, SEARCH(“apple”, A1), 5)

This formula finds “apple” in cell A1 and extracts the next 5 characters.

Using the mid function and returning a known value for 'apple'

For error handling, wrap your search function in IFERROR:

=IFERROR(SEARCH(“apple”, A1), “Not found”)

This returns “Not found” if “apple” isn’t in cell A1.

If error function returning not found.

Creating a Basic Search Box in Excel

Let’s create a simple search functionality without VBA.

Setting Up Your Spreadsheet

  1. Organize your data in a table format
  2. Designate cell A1 as your search input cell
  3. Use column B for your search results

Implementing a Simple Search Formula

In cell B2, enter this formula:

=IFERROR(INDEX($D$2:$D$100, MATCH(“*”&$A$1&”*”, $C$2:$C$100, 0)), “No match found”)

If error, return dessert

This formula:

  1. Searches for partial matches using wildcards
  2. Uses INDEX and MATCH for flexible searching
  3. Returns “No match found” if there’s no result

Enhancing Your Search with Data Validation

To create a drop-down list for search categories:

  1. Select the cell for your drop-down (e.g., A2)
  2. Go to Data > Data Validation
Selecting data validation from the top down
  1. Set “Allow” to “List
Selecting lists from the dropdown.
  1. In “Source“, enter your category range (e.g., $E$2:$E$5)
selecting the range as a source.

Modify your search formula to include the category:

=IFERROR(INDEX($D$2:$D$100, MATCH(1, ($C$2:$C$100=$A$2)*($D$2:$D$100&”*”&$A$1&”*”), 0)), “No match found”)

No match found being returned.

Advanced Search Techniques Without VBA

Utilizing VLOOKUP for Search Functionality

VLOOKUP can be powerful for searching, especially with sorted data.

Syntax:

VLOOKUP(lookup_value, table_array, col_index_num, [range_lookup])

Example for flexible searching:

=IFERROR(VLOOKUP(“*”&A1&”*”, C2:D100, 2, FALSE), “Not found”)

This searches for partial matches of A1 in column C and returns the corresponding value from column D.

Fruits being returned from the if error function

Creating a Dynamic Search with Array Formulas

For multi-criteria searches, use SUMPRODUCT:

=IFERROR(INDEX($D$2:$D$100, MATCH(1, SUMPRODUCT(–($C$2:$C$100=$A$2), –($D$2:$D$100&”*”&$A$1&”*”)), 0)), “No match found”)

The if error function is showing "no match found" once again.

This formula:

  1. Checks if the category in A2 matches column C
  2. Looks for partial matches of A1 in column D
  3. Returns the first match found

Leveraging Power Query for Complex Searches

Power Query offers robust search capabilities for large datasets:

  1. Select your data range
  2. Go to Data > Power Query
Launching Power Query Editor from the menu.
  1. In Power Query Editor, add a “custom column” with this formula:

    if Text.Contains([Column1], “search_term”) then “Match” else “No Match”
  2. Filter the new column to show only “Match” rows
  3. Close and Load to create a dynamic, searchable table

Building a Search Box with VBA

Introduction to VBA in Excel

To access the Visual Basic Editor:

  1. Enable the Developer tab (File > Options > Customize Ribbon)
  2. Click “Visual Basic” on the Developer tab
Selecting 'Developer' from the menu.

Creating a Simple Search Macro

Here’s a basic VBA search function:

vba

Sub SearchData()

    Dim searchTerm As String

    Dim searchRange As Range

    Dim cell As Range

    Dim found As Boolean

    searchTerm = InputBox(“Enter search term:”)

    Set searchRange = Range(“A1:A100”)

    found = False

    For Each cell In searchRange

        If InStr(1, cell.Value, searchTerm, vbTextCompare) > 0 Then

            MsgBox “Found in cell ” & cell.Address

            found = True

            Exit For

        End If

    Next cell

    If Not found Then MsgBox “Not found”

End Sub

Creating a simple search macro?

To use this macro:

  1. Enter the code in a module
  2. Create a button (Developer > Button)
Selecting button from the menu.
  1. Assign the macro to the button

Advanced VBA Search Techniques

For fuzzy matching, you can implement the Levenshtein distance algorithm in VBA:

vba

Function LevenshteinDistance(s1 As String, s2 As String) As Long

    Dim i As Long, j As Long, cost As Long

    Dim d() As Long

    Dim min1 As Long, min2 As Long, min3 As Long

Coefficient Excel Google Sheets Connectors
Try the Free Spreadsheet Extension Over 500,000 Pros Are Raving About

Stop exporting data manually. Sync data from your business systems into Google Sheets or Excel with Coefficient and set it on a refresh schedule.

Get Started

    ReDim d(Len(s1), Len(s2))

    For i = 0 To Len(s1)

        d(i, 0) = i

    Next

    For j = 0 To Len(s2)

        d(0, j) = j

    Next

    For i = 1 To Len(s1)

        For j = 1 To Len(s2)

            If Mid(s1, i, 1) = Mid(s2, j, 1) Then

                cost = 0

            Else

                cost = 1

            End If

            min1 = d(i – 1, j) + 1

            min2 = d(i, j – 1) + 1

            min3 = d(i – 1, j – 1) + cost

            d(i, j) = WorksheetFunction.Min(min1, min2, min3)

        Next j

    Next i

    LevenshteinDistance = d(Len(s1), Len(s2))

End Function

Use this function in your search macro to find close matches.

Implementing the distance algorithm in VBA.

Optimizing Search Performance in Large Datasets

Data Cleansing for Improved Search Accuracy

  1. Remove duplicates: Data > Remove Duplicates
Removing the duplicates.
  1. Standardize data: Use functions like PROPER, TRIM, and CLEAN
  2. Implement data validation to maintain consistency

Indexing and Sorting for Faster Searches

  1. Create helper columns with key information
  2. Sort your data: Data > Sort
Selecting the sort from the menu.
  1. Use binary search in VBA for large datasets:

vba

Function BinarySearch(arr As Variant, searchValue As Variant) As Long

    Dim low As Long, high As Long, mid As Long

    low = LBound(arr)

    high = UBound(arr)

    While low <= high

        mid = (low + high)  2

        If arr(mid) = searchValue Then

            BinarySearch = mid

            Exit Function

        ElseIf arr(mid) < searchValue Then

            low = mid + 1

        Else

            high = mid – 1

        End If

    Wend

    BinarySearch = -1 ‘ Not found

End Function

Using binary search for VBA on large data sets.

Using Pivot Tables for Searchable Summaries

  1. Create a pivot table: Insert > PivotTable
Selecting 'Pivot Table' from the menu.
  1. Add slicers: PivotTable Analyze > Insert Slicer
Selecting pivot table analyze.
  1. Use GETPIVOTDATA for dynamic searches based on pivot table data

Creating an Interactive Dashboard with Search Capabilities

Designing the Dashboard Layout

  1. Plan your layout on paper first
  2. Use shapes and form controls for a user-friendly interface
  3. Incorporate charts that update based on search results

Implementing Dynamic Charts

  1. Create named ranges for your data
  2. Use OFFSET to make ranges dynamic:

    =OFFSET(Sheet1!$A$1,0,0,COUNTA(Sheet1!$A:$A),1)
Using the offset function in returning apple.
  1. Base your charts on these dynamic ranges

Adding Interactivity with Slicers

  1. Create a pivot table from your data
  2. Insert slicers for key fields
  3. Connect slicers to multiple pivot tables: right-click slicer > Report Connections

Creating Search Functions in Excel

By mastering these techniques, you’ll be able to create powerful, efficient search functions in Excel that can handle even the most complex datasets. Remember, the key to a great search function is understanding your data and choosing the right approach for your specific needs.

Ready to take your Excel data management to the next level? Get started with Coefficient to unlock advanced data integration and real-time syncing capabilities that will supercharge your spreadsheets and dashboards.

Connect Live Data to Excel Instantly

Automatically sync data from any source into Excel and keep it on a refresh schedule with Coefficient.

Try the Spreadsheet Automation Tool Over 500,000 Professionals are Raving About

Tired of spending endless hours manually pushing and pulling data into Google Sheets? Say goodbye to repetitive tasks and hello to efficiency with Coefficient, the leading spreadsheet automation tool trusted by over 350,000 professionals worldwide.

Sync data from your CRM, database, ads platforms, and more into Google Sheets in just a few clicks. Set it on a refresh schedule. And, use AI to write formulas and SQL, or build charts and pivots.

Hannah Recker Growth Marketer
Hannah Recker was a data-driven growth marketer before partying in the data became a thing. In her 12 years experience, she's become fascinated with the way data enablement amongst teams can truly make or break a business. This fascination drove her to taking a deep dive into the data industry over the past 4 years in her work at StreamSets and Coefficient.
500,000+ happy users
Wait, there's more!
Connect any system to Google Sheets in just seconds.
Get Started Free

Trusted By Over 50,000 Companies