Excel VBA String Index: Find Characters Easily
In this article
Excel VBA does not have a built-in string indexer like some languages, but you can get any character or substring by position using the Mid function, and find a position using InStr or InStrRev. The quickest recipe is Mid(myString, startPosition, length) for reading characters and InStr(myString, searchText) for locating them. This guide shows both, plus the off-by-one traps that catch most people.
What You'll Need
- Excel with macros enabled (Windows or Mac). The VBA editor is opened with Alt+F11 on Windows, or via Tools > Macro > Visual Basic Editor on Mac.
- A workbook saved as .xlsm if you want to keep your code.
- Basic familiarity with variables and the Immediate window (Ctrl+G in the editor).
Step-by-Step
- Open the VBA editor and insert a module. Press Alt+F11, then in the Project Explorer right-click your workbook and choose Insert > Module. Menu labels can differ slightly by Excel version and region, but the Insert menu is present in all current versions.
- Declare a string and assign a value. In the module, type
Dim s As Stringthens = "Hello World". VBA strings are 1-based, so the first character is at index 1, not 0. This is the single biggest difference from languages like JavaScript or Python. - Read a single character with Mid. Use
Mid(s, 1, 1)to get "H",Mid(s, 7, 1)to get "W". The syntax isMid(string, start, length). If you omit length, Mid returns everything from start to the end of the string, soMid(s, 7)gives "World". - Read a substring.
Mid(s, 1, 5)returns "Hello".Mid(s, 7, 5)returns "World". Always check that start plus length minus one does not exceed Len(s), or you will silently get a shorter string rather than an error. - Get the length with Len.
Len(s)returns 11 for "Hello World". Use it to loop safely:For i = 1 To Len(s). Trying to readMid(s, 12, 1)returns an empty string, not an error, so Len is your guard. - Find a character position with InStr.
InStr(s, "W")returns 7. The syntax isInStr([start], string, substring, [compare]). It returns 0 when the text is not found, so always testIf pos > 0 Thenbefore using the result in Mid. - Search from the end with InStrRev.
InStrRev(s, "o")returns 8, the last "o". This is useful for pulling a file extension or the text after the final backslash in a path. - Combine InStr and Mid for dynamic extraction. To get everything after the space in "Hello World":
pos = InStr(s, " ")thenresult = Mid(s, pos + 1). The +1 skips the delimiter itself. - Use Left and Right for the ends.
Left(s, 5)gives "Hello";Right(s, 5)gives "World". These are simpler than Mid when you only need the start or end. - Test in the Immediate window. Press Ctrl+G and type
?Mid("Hello World", 7, 5)then Enter. You should see World. This is the fastest way to confirm indexing before wiring code into a macro. - Handle case sensitivity. By default InStr is case-sensitive (vbBinaryCompare). Pass
vbTextCompareas the fourth argument, e.g.InStr(1, s, "world", vbTextCompare), to match regardless of case. - Write results back to a cell.
Range("A1").Value = Mid(s, 7, 5)puts "World" in A1. Save the workbook as .xlsm to keep the macro.
Common Mistakes
- Assuming 0-based indexing. VBA strings start at 1.
Mid(s, 0, 1)raises a runtime error 5 (Invalid procedure call or argument). - Forgetting InStr returns 0 when not found. Using that 0 in Mid causes an error. Always branch on
pos > 0. - Mixing up start and length. The second argument of Mid is the starting position, the third is how many characters, not the end position.
Mid(s, 1, 5)is characters 1 through 5. - Ignoring trailing spaces. Imported data often has stray spaces that shift every index. Run
Trim(s)first if positions look wrong. - Using InStr with a start position of 0. The optional start must be 1 or greater; 0 triggers an error.
- Expecting an error on out-of-range reads. Mid past the end returns an empty string, which can hide bugs. Check Len first.
Tips
- Use
Option Compare Textat the top of the module to make all string comparisons case-insensitive by default. - Store positions in a Long, not an Integer, when working with long strings or many rows.
- For repeated parsing, write a small helper function that wraps InStr and Mid and returns the piece you need.
- Use
Split(s, " ")when you need every word; it returns a zero-based array, unlike string positions. - Use
Replaceto strip characters before indexing if the format is inconsistent. - Test edge cases: empty string, delimiter at the start, delimiter at the end, and no delimiter at all.
Troubleshooting
If you get runtime error 5, check that your start position is at least 1 and your length is not negative. If Mid returns an empty string, your start is past Len(s) or the variable is empty; print Len(s) in the Immediate window to confirm. If InStr keeps returning 0, the text may differ in case or contain a non-breaking space copied from a web page; try vbTextCompare and Trim. If results look shifted by one, you are probably treating the position as 0-based. If the macro does not run at all, confirm the file is .xlsm and macros are enabled in the Trust Center. If a character such as an accented letter splits oddly, remember VBA's Len and Mid count characters, not bytes, in modern Excel, but very old code using byte functions can behave differently.
Frequently Asked Questions
Is VBA string indexing 0-based or 1-based?
1-based. The first character is at position 1, and position 0 causes an error in Mid and InStr.
How do I get the last character of a string?
Use Right(s, 1), or Mid(s, Len(s), 1). Guard against an empty string by checking Len first.
What is the difference between InStr and InStrRev?
InStr searches from the start (or a given start position) forward; InStrRev searches backward from the end. Both return 0 if not found.
Can I index a string like an array, e.g. s(1)?
No. VBA strings are not arrays of characters. Use Mid, Left, or Right instead.
How do I make InStr case-insensitive?
Pass vbTextCompare as the fourth argument, or add Option Compare Text at the top of the module.
Why does Mid return fewer characters than I asked for?
Because the requested length runs past the end of the string. Mid stops at the last character rather than padding or erroring.