Excel VBA InStr Function: Step-by-Step Guide
In this article
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
- 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.
- Write a minimal test. Type this procedure:
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.Sub TestInStr() Dim pos As Long pos = InStr("Hello World", "World") MsgBox pos End Sub - 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 pass1as the start when you supply a compare argument; you cannot skip the start position. - 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 topos + 1. - Test a cell value. Replace the literal with a range reference:
This pattern is the workhorse for filtering, flagging, and conditional formatting logic in VBA.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 - Handle empty strings and Nulls.
InStr("", "a")returns 0. If a cell is empty,Range("A1").Valueis 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 yourIftest fails. Guard withIf Not IsNull(txt) Thenbefore calling InStr. - Use it to split text. To extract everything before a delimiter:
Remember to subtract 1 for the left portion and add 1 for the right.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 - Loop through a column. Combine InStr with a
For Eachloop over a range to process many rows. CheckIf InStr(...) > 0 Thenand act only on matches. Exit the loop or useApplication.ScreenUpdating = Falsefor speed on large ranges. - 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. UseInStr(1, "text", "t", vbTextCompare). - Assuming case-insensitivity. The default is binary compare, so "Apple" and "apple" do not match. Pass
vbTextComparewhen 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 ofLeft(txt, p - 1)includes the delimiter in the left part. - Ignoring Null. A Null input makes the whole expression Null, and your
Ifsilently 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
InStrRevto find the last occurrence, handy for file extensions and paths. - For simple existence checks,
InStr(...) > 0is 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$andLCase$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.