Smart Systems for Home

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

Excel VBA InStr: Find Text in a String

· 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

InStr is the VBA function that tells you where one string first appears inside another. It returns the starting character position as a number, or 0 if the text is not found. The basic form is InStr(1, "Hello World", "World"), which returns 7 because the W is the seventh character. You can use it in any VBA-enabled Excel workbook (the .xlsm format) to test whether a cell contains certain text, split values, validate input, or drive If/Then logic. No add-ins are required, but you must enable the Developer tab or press Alt+F11 to open the VBA editor.

What You'll Need

  • Excel for Windows or Mac with macros enabled (the workbook must be saved as .xlsm or .xlsb).
  • The Developer tab visible, or the ability to press Alt+F11 to open the Visual Basic Editor.
  • A basic understanding of how to insert a module and run a macro.
  • Optional: sample text in cells A1:A10 so you can test the examples.

Step-by-Step

  1. Open the VBA editor. Press Alt+F11 (Windows) or Fn+Alt+F11 (some Mac keyboards). If the Developer tab is not visible, enable it via File > Options > Customize Ribbon and tick Developer, or on Mac via Excel > Preferences > Ribbon & Toolbar.
  2. Insert a standard module. In the editor, choose Insert > Module. A blank code window appears. All the code below goes there.
  3. Write your first InStr test. Type this procedure:
    Sub FindText()
    Dim pos As Long
    pos = InStr(1, "Hello World", "World")
    MsgBox pos
    End Sub

    Press F5 to run it. The message box shows 7. If you change "World" to "world", it returns 0 because InStr is case-sensitive by default.
  4. Understand the arguments. The full syntax is InStr([start], string1, string2, [compare]). start is the character position to begin searching (use 1 to search from the beginning). string1 is the text being searched. string2 is the text you are looking for. compare is optional: use vbTextCompare for case-insensitive matching or vbBinaryCompare for case-sensitive (the default). Example: InStr(1, "Hello", "h", vbTextCompare) returns 1.
  5. Use InStr with cell values. Replace the literal strings with Range references:
    Sub CheckCell()
    Dim txt As String
    txt = Range("A1").Value
    If InStr(1, txt, "error", vbTextCompare) > 0 Then
    MsgBox "Found the word error"
    Else
    MsgBox "Not found"
    End If
    End Sub

    This is the most common real-world pattern: test whether a cell contains a substring and branch on the result.
  6. Start searching from a later position. If you need to find the second occurrence, set start to the position after the first match. For example, firstPos = InStr(1, txt, "a") then secondPos = InStr(firstPos + 1, txt, "a"). If secondPos is 0, there is no second match.
  7. Extract text around the match. Combine InStr with Left, Mid, and Len. To get everything after the match: Mid(txt, InStr(1, txt, "=") + 1). To get everything before: Left(txt, InStr(1, txt, "=") - 1). Guard against 0 by checking the position first, otherwise you will get an error or an empty string.
  8. Loop through a range. Use a For Each loop to apply the test to many cells:
    Sub FlagCells()
    Dim c As Range
    For Each c In Range("A1:A10")
    If InStr(1, c.Value, "urgent", vbTextCompare) > 0 Then
    c.Interior.Color = vbYellow
    End If
    Next c
    End Sub

    This highlights every cell containing "urgent" regardless of case.
  9. Save and test. Save the workbook as .xlsm. Close the editor and run the macro from Developer > Macros or assign it to a button. Check the results against your sample data.

Common Mistakes

  • Forgetting that InStr is case-sensitive by default. "Excel" and "excel" return different results unless you pass vbTextCompare. Always specify the compare argument when case should not matter.
  • Comparing the result to True/False. InStr returns a number, not a Boolean. Use If InStr(...) > 0 Then, not If InStr(...) = True.
  • Using the wrong argument order. The searched string comes second, the search term third. Swapping them gives wrong positions or 0.
  • Not guarding against 0. If you do Mid(txt, InStr(...) + 1) and the term is missing, InStr returns 0 and Mid starts at position 1, silently returning the whole string. Check the position first.
  • Assuming InStr finds the last occurrence. It finds the first match from the start position. To find the last, loop with InStrRev or keep advancing the start position.

Tips

  • Use InStrRev when you need the last occurrence, for example to get a file extension after the final dot.
  • Store the result in a Long variable rather than calling InStr repeatedly; it is easier to debug and faster in loops.
  • For simple yes/no tests you can also use the Like operator or InStr with > 0; both are readable, but InStr gives you the position for further extraction.
  • When working with cell values, trim them first with Trim(c.Value) to avoid leading or trailing spaces affecting your search.
  • Test with an empty string: InStr(1, "", "a") returns 0, which is safe to handle with the same guard.

Troubleshooting

If the macro does not run, check that macros are enabled in the Trust Center (File > Options > Trust Center > Trust Center Settings > Macro Settings). If you see a "Sub or Function not defined" error, confirm the code is in a standard module, not a sheet module, and that you spelled InStr correctly. If the result is always 0, verify the search term is not empty and that you are not searching a numeric cell without converting it with CStr. If positions look off by one, remember InStr counts from 1, not 0. On Mac, some keyboard shortcuts differ, but the function behaves the same. If the workbook is shared or protected, unprotect it before running code that modifies cells.

Frequently Asked Questions

Is InStr case-sensitive?

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

What does InStr return when the text is not found?

It returns 0. Always test with > 0 before using the position.

How do I find the second occurrence?

Call InStr again with the start argument set to the first position plus one.

Can InStr search from the end?

No, use InStrRev for a reverse search from the end of the string.

Does InStr work with numbers?

It expects strings. Convert numbers with CStr first, or the function may coerce them unpredictably.

What is the difference between InStr and InStrRev?

InStr searches forward from the start position; InStrRev searches backward from the end and returns the position of the last match.