Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA InStr Function: Step-by-Step Guide

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

The Excel VBA InStr function returns the starting position of one string inside another, or 0 if the substring is not found. Use it to test whether a cell contains a word, to split text at a delimiter, or to drive loops that scan a column. The syntax is InStr([start], string1, string2, [compare]): start is an optional character position to begin searching, string1 is the text to search, string2 is what you are looking for, and compare is optional (vbBinaryCompare, the default, or vbTextCompare for case-insensitive matching). The function returns a Long. You need Excel with the Developer tab enabled and a basic understanding of where to put VBA code.

What You'll Need

  • Excel for Windows or Mac (the VBA editor is included; no add-in required).
  • The Developer tab visible: File > Options > Customize Ribbon, tick Developer. On Mac, Excel > Preferences > Ribbon & Toolbar.
  • A workbook saved as .xlsm if you want to keep macros.
  • Basic familiarity with opening the VBA editor (Alt+F11 on Windows, Option+F11 on Mac) and inserting a module.

Step-by-Step

  1. Open the VBA editor. Press Alt+F11 (Windows) or Option+F11 (Mac). In the Project pane, right-click your workbook and choose Insert > Module. A blank code window appears.
  2. Write a minimal test. Type this procedure:
    Sub TestInStr()
        Dim pos As Long
        pos = InStr("Hello World", "World")
        MsgBox pos
    End Sub
    Press F5. The message box shows 7, because "World" starts at the seventh character. If you see 0, the substring was not found or the compare mode is wrong.
  3. Make it case-insensitive. Add the fourth argument: InStr(1, "Hello World", "world", vbTextCompare) returns 7. Without it, the default binary compare returns 0 because the case differs. Always pass 1 as the start when you supply a compare argument; you cannot skip the start position.
  4. Search from a later position. Use the start argument to skip earlier matches: InStr(8, "banana", "a") returns 10 (the third "a"). This is how you loop through repeated occurrences: after a match, set the next start to pos + 1.
  5. Test a cell value. Replace the literal with a range reference:
    Sub CheckCell()
        Dim txt As String
        txt = Range("A1").Value
        If InStr(1, txt, "urgent", vbTextCompare) > 0 Then
            MsgBox "Contains the keyword"
        Else
            MsgBox "Keyword not found"
        End If
    End Sub
    This pattern is the workhorse for filtering, flagging, and conditional formatting logic in VBA.
  6. Handle empty strings and Nulls. InStr("", "a") returns 0. If a cell is empty, Range("A1").Value is an empty string, which is safe. But if the cell contains an error or the variable is Null (common with database queries), InStr returns Null and your If test fails. Guard with If Not IsNull(txt) Then before calling InStr.
  7. Use it to split text. To extract everything before a delimiter:
    Dim p As Long
    p = InStr(1, txt, "-", vbTextCompare)
    If p > 0 Then
        leftPart = Left$(txt, p - 1)
        rightPart = Mid$(txt, p + 1)
    End If
    Remember to subtract 1 for the left portion and add 1 for the right.
  8. Loop through a column. Combine InStr with a For Each loop over a range to process many rows. Check If InStr(...) > 0 Then and act only on matches. Exit the loop or use Application.ScreenUpdating = False for speed on large ranges.
  9. Save and test. Save the workbook as .xlsm. Run the macro with Alt+F8, select the procedure, and click Run. Step through with F8 to watch the return value in the Locals window.

Common Mistakes

  • Forgetting the start argument when using compare. InStr("text", "t", vbTextCompare) is a syntax error because the third argument must be the search string. Use InStr(1, "text", "t", vbTextCompare).
  • Assuming case-insensitivity. The default is binary compare, so "Apple" and "apple" do not match. Pass vbTextCompare when case should not matter.
  • Comparing to True/False. InStr returns a number, not a Boolean. Test > 0, not = True.
  • Off-by-one in splitting. Using Left(txt, p) instead of Left(txt, p - 1) includes the delimiter in the left part.
  • Ignoring Null. A Null input makes the whole expression Null, and your If silently takes the Else branch. Check for Null first.
  • Using InStr on numbers without converting. InStr expects strings; passing a numeric variant can raise a type mismatch. Use CStr().

Tips

  • Use InStrRev to find the last occurrence, handy for file extensions and paths.
  • For simple existence checks, InStr(...) > 0 is faster and clearer than building a loop.
  • Store the compare mode in a constant if you use it repeatedly, so you do not accidentally mix binary and text compares.
  • Combine with Trim$ and LCase$ for robust matching when data is messy.
  • When scanning large ranges, read the range into a Variant array first and loop the array; cell-by-cell InStr calls are slow.

Troubleshooting

If InStr always returns 0, check the compare argument first: a case mismatch is the most common cause. Next, confirm the search string has no leading or trailing spaces; use Trim$ on both sides. If you get a compile error, verify the argument order and that you supplied the start position when using compare. If you get a type mismatch, one of the inputs is not a string or is Null; wrap with CStr and guard with IsNull. If the macro runs but the result looks wrong, step through with F8 and inspect the variables in the Locals window. If nothing runs at all, check that macros are enabled in the Trust Center and that the code is in a standard module, not a sheet module, unless you intended a sheet event.

Frequently Asked Questions

What does InStr return if the substring is not found?

It returns 0. If the input is Null, it returns Null.

Is InStr case-sensitive?

By default yes (binary compare). Pass vbTextCompare as the fourth argument to make it case-insensitive.

Can I use InStr to check if a cell contains a word?

Yes. Test If InStr(1, Range("A1").Value, "word", vbTextCompare) > 0 Then.

What is the difference between InStr and InStrRev?

InStr searches from the start (or a given position) forward; InStrRev searches backward from the end and returns the position of the last match.

Why does InStr give a syntax error with four arguments?

You likely omitted the start position. When you supply compare, you must also supply start, e.g. InStr(1, text, find, vbTextCompare).

Can InStr handle wildcards?

No. InStr does a literal substring match. For pattern matching, use the Like operator or RegExp.