Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 535421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:42:53+00:00 2026-05-13T09:42:53+00:00

How good/fast is Excel VBA’s Find vs. binary search? My platform is Office 11|2003

  • 0

How good/fast is Excel VBA’s Find vs. binary search? My platform is Office 11|2003 and I’ll be searching for strings against Column A on three sheets of values. Total number of rows ~140,000

If worth it which Library & functions should I reference to do the sorting and then the binary search? Binary searching strings/text reportedly has potential problems.

… one thing
must be noted. Using binary search
formulas with sortedtextrequires
caution. Aladin A., Excel MVP

Excel Find:

Worksheets(1).Range("A:A").Find("PN-String-K9", LookIn:=xlValues, LookAt:=xlWhole)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T09:42:54+00:00Added an answer on May 13, 2026 at 9:42 am

    Much against my intuition a VBA binary search strongly outperforms an Excel Find. At least with the scenario below where 120,000 6 character strings are distributed evenly over 3 worksheets.

    Excel Find takes 1 minute 58 seconds,
    VBA binary search takes 36 seconds on my particular machine.

    The advantage of knowing that the text is in order obviously outweighs Excel’s natural advantage. Note Aladin A’s warning about sort order.

    Option Explicit
    
    ' Call Search to look for a thousand random strings
    ' in 3 worksheets of a workbook
    
    ' requires a workbook with 3 sheets and
    ' column A populated with values between "00001" to "120000"
    ' split evenly 40,000 to a worksheet in ascending order.
    ' They must be text, not numbers.
    
    Private Const NUM_ROWS As Long = 120000
    Private Const SHEET_1 As String = "Sheet1"
    Private Const SHEET_2 As String = "Sheet2"
    Private Const SHEET_3 As String = "Sheet3"
    
    ' This uses VBA Binary Search
    Public Sub Search()
        Worksheets(SHEET_1).Range("B:B").ClearContents
        Worksheets(SHEET_2).Range("B:B").ClearContents
        Worksheets(SHEET_3).Range("B:B").ClearContents
        DoSearch True       ' change to False to test Excel search
    End Sub
    
    ' Searches for a thousand values using binary  or excel search depending on
    ' value of bBinarySearch
    Public Sub DoSearch(ByVal bBinarySearch As Boolean)
        Debug.Print Now
        Dim ii As Long
    
        For ii = 1 To 1000
            Dim rr As Long
            rr = Int((NUM_ROWS) * Rnd + 1)
            If bBinarySearch Then
                Dim strSheetName As String
                Dim nRow As Long
                If BinarySearch(MakeSearchArg(rr), strSheetName, nRow) Then
                    Worksheets(strSheetName).Activate
                    Cells(nRow, 1).Activate
                End If
            Else
                If Not ExcelSearch(SHEET_1, MakeSearchArg(rr)) Then
                    If Not ExcelSearch(SHEET_2, MakeSearchArg(rr)) Then
                        ExcelSearch SHEET_3, MakeSearchArg(rr)
                    End If
                End If
            End If
            ActiveCell.Offset(0, 1).Value = "FOUND"
        Next
        Debug.Print Now
    
    End Sub
    
    ' look for one cell value using Excel Find
    Private Function ExcelSearch(ByVal strWorksheet As String _
      , ByVal strSearchArg As String) As Boolean
        On Error GoTo Err_Exit
        Worksheets(strWorksheet).Activate
        Worksheets(strWorksheet).Range("A:A").Find(What:=strSearchArg, LookIn:=xlValues, LookAt:= 
            xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True 
            , SearchFormat:=False).Activate
        ExcelSearch = True
        Exit Function
    Err_Exit:
        ExcelSearch = False
    End Function
    
    ' Look for value using a vba based binary search
    ' returns true if the search argument is found in the workbook
    ' strSheetName contains the name of the worksheet on exit and nRow gives the row
    Private Function BinarySearch(ByVal strSearchArg As String _
      , ByRef strSheetName As String, ByRef nRow As Long) As Boolean
        Dim nFirst As Long, nLast As Long
        nFirst = 1
        nLast = NUM_ROWS
        Do While True
            Dim nMiddle As Long
            Dim strValue As String
            If nFirst > nLast Then
                Exit Do     ' Failed to find search arg
            End If
            nMiddle = Round((nLast - nFirst) / 2 + nFirst)
            SheetNameAndRowFromIdx nMiddle, strSheetName, nRow
            strValue = Worksheets(strSheetName).Cells(nRow, 1)
            If strSearchArg < strValue Then
                nLast = nMiddle - 1
            ElseIf strSearchArg > strValue Then
                nFirst = nMiddle + 1
            Else
                BinarySearch = True
                Exit Do
            End If
        Loop
    End Function
    
    ' convert 1 -> "000001", 120000 -> "120000", etc
    Private Function MakeSearchArg(ByVal nArg As Long) As String
        MakeSearchArg = Right(CStr(nArg + 1000000), 6)
    End Function
    
    ' converts some number to a worksheet name and a row number
    ' This is depenent on the worksheets being named sheet1, sheet2, sheet3
    
    ' and containing an equal number of vlaues in each sheet where
    ' the total number of values is NUM_ROWS
    Private Sub SheetNameAndRowFromIdx(ByVal nIdx As Long _
      , ByRef strSheetName As String, ByRef nRow As Long)
        If nIdx <= NUM_ROWS / 3 Then
    
            strSheetName = SHEET_1
            nRow = nIdx
        ElseIf nIdx > (NUM_ROWS / 3) * 2 Then
            strSheetName = SHEET_3
            nRow = nIdx - (NUM_ROWS / 3) * 2
        Else
            strSheetName = SHEET_2
            nRow = nIdx - (NUM_ROWS / 3)
        End If
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 270k
  • Answers 270k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Strictly speaking, you don't need to do anything to a… May 13, 2026 at 1:32 pm
  • Editorial Team
    Editorial Team added an answer Absolutely! You should also use the ignore_user_abort() function to make… May 13, 2026 at 1:32 pm
  • Editorial Team
    Editorial Team added an answer No, there aren't any. And you can't call the static… May 13, 2026 at 1:32 pm

Related Questions

The Situation: I'm optimizing a pure-java implementation of the LZF compression algorithm, which involves
We're using Fast Reports to create reports but we're not very happy with the
This is basically a math problem, but very programing related: if I have 1
I am using VMware Server 1.0.7 on Windows XP SP3 at the moment to
I was just reading this: http://www.tvidesign.co.uk/blog/improve-your-jquery-25-excellent-tips.aspx And had some questions about some of the

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.