Excel VBA String Functions: Left, Right, Mid, Len
In this article
Excel VBA string functions let you slice, measure and test text inside macros. The four you will use most are Left, Right, Mid and Len: Left returns characters from the start, Right from the end, Mid from anywhere in the middle, and Len counts total characters. You can try them immediately in the Visual Basic Editor (Alt+F11) by opening the Immediate Window (Ctrl+G) and typing a line such as ?Left("Excel",2) then pressing Enter, which prints Ex. Nothing needs installing beyond Excel itself.
What You'll Need
- Excel for Windows or Mac with macros enabled. The steps below use the Windows keyboard shortcuts; on Mac, use the menu equivalents (Developer tab, Visual Basic) and note that labels can differ slightly by version, device or region.
- The Developer tab visible: File > Options > Customize Ribbon, tick Developer in the right-hand list. On Mac, Excel > Preferences > Ribbon & Toolbar.
- A workbook saved as .xlsm (macro-enabled) if you intend to keep your code.
Step-by-Step
- Open the editor. Press Alt+F11 to open the Visual Basic Editor. If the Immediate Window is not visible, press Ctrl+G. This window is the fastest place to test a function before you commit it to a macro.
- Test Len first. Type
?Len("Smart Home")and press Enter. The result is 10, because Len counts every character including the space. Len is the measuring stick for the other three functions, so get comfortable with it. - Test Left. Type
?Left("Smart Home",5)and press Enter. The result is Smart. Left takes the number of characters you specify, counting from the first character. If you ask for more characters than the string contains, Left simply returns the whole string rather than an error. - Test Right. Type
?Right("Smart Home",4)and press Enter. The result is Home. Right counts backwards from the final character. Like Left, it returns the whole string if the count exceeds the length. - Test Mid. Type
?Mid("Smart Home",7,4)and press Enter. The result is Home. Mid takes a start position and a length. The start position is 1-based, so position 7 is the H. If you omit the length, Mid returns everything from the start position to the end:?Mid("Smart Home",7)also gives Home. - Combine them with Len. A common pattern is extracting everything after a separator. Type
?Mid("Living Room Light", Len("Living Room ")+1)and press Enter. Len finds where the space ends, and adding 1 starts Mid on the next character. The result is Light. This is how you strip a prefix without hard-coding a position. - Use variables in a real macro. In a standard module (Insert > Module), write a short procedure: declare
Dim s As String, assigns = "Kitchen Sensor 12", then useLeft(s, 7),Right(s, 2)andMid(s, 9, 6)to pull out Kitchen, 12 and Sensor. Use Debug.Print to send each result to the Immediate Window, or assign to a cell withRange("A1").Value = Left(s, 7). - Guard against empty strings. Wrap calls in a check such as
If Len(s) > 0 Thenbefore slicing. Len returns 0 for an empty string, and a zero-length result from Left, Right or Mid is legal but often a sign that your source data was blank. - Handle errors from bad start positions. Mid raises a run-time error if the start argument is less than 1, and also if it is greater than the length of the string. Test the position against Len before calling Mid, or use
On Errorhandling in the procedure. - Run and save. Press F5 to run the macro, check the Immediate Window or your worksheet cell, then save the workbook as .xlsm so the code is preserved.
Common Mistakes
- Assuming Mid is zero-based. It is not. Mid("Excel",0,2) errors; the first character is position 1.
- Forgetting spaces count. Len("Smart Home") is 10, not 9. A trailing space in imported data shifts every position by one.
- Mixing up Left and Right counts. Left("Excel",2) gives Ex; Right("Excel",2) gives el. They are not interchangeable.
- Passing a number instead of text. Left, Right, Mid and Len expect a string. VBA usually coerces a number, but leading zeros are lost, so convert with CStr first if the zeros matter.
- Omitting the length in Mid. This is valid and returns the rest of the string, but many people expect an error and add a guessed number instead.
- Using these on cell ranges. They work on a single string value, not a whole range. Loop through cells or read each cell's Value into a variable first.
Tips
- Use InStr to find a separator's position, then feed that into Mid or Left instead of hard-coding numbers. For example,
Left(s, InStr(s, " ")-1)returns the first word. - Trim imported text with Trim before slicing, so stray spaces do not throw off your counts.
- Keep test strings in the Immediate Window short and obvious, like "Excel", so you can see at a glance whether the result is right.
- Comment each hard-coded position in your macro. Six months later, Mid(s, 9, 6) tells you nothing without a note.
- For case-insensitive searches before slicing, use InStr with the compare argument set to vbTextCompare.
Troubleshooting
- Nothing prints in the Immediate Window. Make sure you typed the question mark before the function name, and that the window is actually open (Ctrl+G).
- Run-time error 5 on Mid. The start position is below 1 or beyond the string length. Check Len first and adjust.
- The result is shorter than expected. The source string probably contains fewer characters than you assumed, often because a trailing space was trimmed on import or a value was blank.
- Macro will not run at all. Check that macros are enabled and the file is saved as .xlsm. If the Developer tab is missing, re-enable it through Options as described above.
- Unexpected characters at the start. Hidden characters such as non-breaking spaces can survive Trim. Inspect the string with Len and a loop, or clean the data at the source.
Frequently Asked Questions
Are VBA Left, Right and Mid the same as the Excel worksheet functions?
They behave the same way for normal text, but in VBA you call them as functions in code, not as cell formulas. The worksheet versions are entered in a cell; the VBA versions run inside a macro.
Is Mid zero-based like some other languages?
No. VBA's Mid uses 1 for the first character. A start of 0 causes an error.
What happens if I ask Left for more characters than exist?
It returns the entire string without an error. The same applies to Right.
Can I use these functions on a whole column at once?
Not directly. They work on one string at a time, so loop through the cells or read each cell's value into a variable and process it.
How do I extract text after a space?
Combine Mid with InStr: find the space position with InStr, add 1, and pass that as the start argument to Mid. That avoids hard-coding a position.
Do these functions work on numbers?
VBA usually converts the number to text automatically, but leading zeros are dropped. Convert explicitly with CStr if the zeros matter.