Searching Very Large Strings in a Macro

Written by Allen Wyatt (last updated March 15, 2025)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, 2021, 2024, and Excel in Microsoft 365


2

In his macro, Ian needs to locate a short string within a very large string. The large string is essentially the text of an entire file, sometimes more than 180,000 characters. Neither Search() nor Find() works, unless he chops the large string into smaller parts (e.g., under 32,000 characters), and searches each part in turn. The problem with this dissection is that Ian doesn't know where within the file his target text may lie, or even whether it's in there, and it's quite possible that he might chop his target text and never be able to find it.

There are three general ways that you can search for the instance of one string within another in VBA:

  • Find method
  • Search method
  • InStr function

The first two approaches are the ones that Ian specifically mentions, so he knows how to use these. They are actually methods, used with the WorksheetFunction object. They are essentially used to access the FIND and SEARCH worksheet functions from within VBA.

They aren't the best for finding information in a large, large string. For that you should use the InStr function, a function native to VBA. This is particularly true if your large, large string is being pulled in from a text file and does not actually reside within a workbook.

Let's say that you have a large text file you want to search for a particular text value. You can read the entire file into memory at one time in this manner:

Sub CheckFullFile()
    Dim sFullFile As String
    Dim sFindText As String
    Dim lFileSize As Long
    Dim lStart As Long
    Dim lLoc As Long
    Dim sMsg As String

    ' This is what you want to search for, in lowercase
    sFindText = "mytext"

    Open "c:\ReallyBigFile.txt" For Input As 1
    lFileSize = Lof(1)
    sFullFile = Input(lFileSize, 1)
    Close 1
    sFullFile = LCase(sFullFile)

    lStart = 0
    lLoc = InStr(sFullFile, sFindText)
    While lLoc > 0
        sMsg = sMsg & "Found at " & lLoc & vbCrLf
        lStart = lLoc + 1
        lLoc = InStr(lStart, sFullFile, sFindText)
    Wend
    MsgBox sMsg
End Sub

The macro opens the text file specified, stuffs the entire file contents into the sFullFile string, and displays all instances of the sFindText variable within that text. Note that the file contents are converted to lowercase. This is necessary because the InStr function is case sensitive. If you want your search to be case sensitive, you don't need to do the conversion.

The only effective limitation on code like this is the amount of memory you have in your system. VBA has an effective string length limitation of somewhere over 2 billion characters, but few systems have that much RAM in them. Of course, most text files you would be reading do not approach that file size, either. Testing with your actual data files will let you know if this read-n-the-full-file approach will work for you.

If you have a text file that has individual lines of text in it, as most do, then there is a more efficient way to process the file and look for your text. (Individual lines are those terminated with either a carriage return and/or line feed.)

Sub CheckEachLine()
    Dim sRaw As String
    Dim sFindText As String
    Dim lFileSize As Long
    Dim lStart As Long
    Dim lLoc As Long
    Dim sMsg As String

    ' This is what you want to search for, in lowercase
    sFindText = "mytext"

    Open "c:\ReallyBigFile.txt" For Input As 1

    Do Until Eof(1)
        Line Input #1, sRaw
        sRaw = LCase(sRaw)

        lStart = 0
        lLoc = InStr(sTemp, sFindText)
        While lLoc > 0
            sMsg = sMsg & "Found at " & lLoc & vbCrLf
            lStart = lLoc + 1
            lLoc = InStr(lStart, sRaw, sFindText)
        Wend
    Loop
    Close 1

    MsgBox sMsg
End Sub

With this approach, you don't have to really worry about the length of the text file since only a single line at a time is being read into memory and processed.

I'm sure you get the idea—InStr is the way to go when you want to work with finding information in amazingly large source strings.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (13660) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, 2021, 2024, and Excel in Microsoft 365.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Spell Checking when Closing Documents

When you close a document, you might want to do one final check of the spelling, just to make sure that you didn't miss ...

Discover More

EOMONTH Function is Flakey

Some users have reported problems using the EOMONTH function in later versions of Excel, beginning with Excel 2007. The ...

Discover More

Excel Dark Mode

All the rave these days seems to be displaying information in either "light mode" or "dark mode." If you are interested ...

Discover More

Program Successfully in Excel! This guide will provide you with all the information you need to automate any task in Excel and save time and effort. Learn how to extend Excel's functionality with VBA to create solutions not possible with the standard features. Includes latest information for Excel 2024 and Microsoft 365. Check out Mastering Excel VBA Programming today!

More ExcelTips (ribbon)

Reversing Names In Place

Do you want a way to reverse names within a cell, making them "last, first" instead of "first last?" Here's a handy macro ...

Discover More

Using R1C1 Formula References in a Macro

Besides the regular way of displaying formulas, Excel can also display them using what is called R1C1 format. If you are ...

Discover More

Displaying the "Last Modified" Date

Want to know when a workbook was last modified? Want to put that date within the header of your worksheet? Here's how to ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 5 - 3?

2025-03-22 12:36:32

Tom Bates

Actually, InStr *can* do case-insensitive compares. Depending on the Office app and the VBA Option statement, the InStr function may *default* to other than binary comparison.


2025-03-15 11:23:31

J. Woolley

The following statements in the CheckEachLine macro

    Do Until Eof(1)
        Line Input #1, sRaw
        sRaw = LCase(sRaw)
        lStart = 0
        lLoc = InStr(sTemp, sFindText)
        While lLoc > 0
            sMsg = sMsg & "Found at " & lLoc & vbCrLf
            lStart = lLoc + 1
            lLoc = InStr(lStart, sRaw, sFindText)
        Wend
    Loop

should be replaced by these statements

    Dim lLine As Long
    Do Until EOF(1)
        Line Input #1, sRaw
        sRaw = LCase(sRaw)
        lLine = lLine + 1
        lLoc = InStr(sRaw, sFindText)
        While lLoc > 0
            sMsg = sMsg & "Found at " & lLoc & " of line " & lLine & vbCrLf
            lLoc = InStr(lLoc + 1, sRaw, sFindText)
        Wend
    Loop


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.