Excel VBA Left Function: Step-by-Step Guide
In this article
The VBA Left function returns a specified number of characters from the start of a text string. Use it as Left(text, length) inside a macro, for example Left("Excel VBA", 5), which returns "Excel". Nothing needs to be installed beyond Excel itself; you just need the Developer tab visible and a standard module to paste code into.
What You'll Need
- Excel for Windows or Mac (the VBA editor is included in both; the menu labels can differ slightly by version, device and region).
- The Developer tab enabled. If you do not see it, right-click the ribbon and choose Customize the Ribbon, then tick Developer in the right-hand list.
- A workbook saved as .xlsm if you want to keep your macro. A normal .xlsx file will drop the code when you close it.
- Basic familiarity with opening the VBA editor using Alt+F11 on Windows, or the equivalent menu command on Mac.
Step-by-Step
- Open the VBA editor. Press Alt+F11 (Windows) or use Tools > Macro > Visual Basic Editor on Mac. The editor opens in its own window.
- Insert a standard module. In the editor, choose Insert > Module. A blank code pane appears on the right. Do not paste code into a sheet module or the ThisWorkbook module unless you specifically want event code there.
- Write a simple test procedure. Type the following, replacing nothing yet:
Sub TestLeft()
MsgBox Left("Excel VBA", 5)
End Sub - Run it. Place the cursor inside the procedure and press F5. A message box should show Excel. If it does, the function is working and you can adapt it.
- Feed it a cell value. Change the line to
MsgBox Left(Range("A1").Value, 5). Put some text in cell A1 first. This is the most common real-world pattern: read a cell, slice the first characters, and do something with the result. - Store the result in a variable. Declare a String variable and assign the output:
Dim part As String
part = Left(Range("A1").Value, 5)
You can then write it back withRange("B1").Value = part. - Handle empty cells and short strings. If A1 is empty, Left returns an empty string. If A1 contains fewer characters than the length you ask for, Left returns the whole string without error. Test both cases before trusting the macro on a full column.
- Loop through a range. For a column of values, use a For Each loop over the used range, apply Left to each cell, and write the result to the adjacent column. Add a check such as
If Len(cell.Value) >= 5 Thenif you only want rows long enough to slice. - Combine with other functions. Left is often paired with InStr to cut text before a delimiter, for example
Left(txt, InStr(txt, "-") - 1). Guard against InStr returning 0, which would produce a negative length and a run-time error. - Save the workbook. Use File > Save As and choose Excel Macro-Enabled Workbook (.xlsm). Close and reopen to confirm the macro is still there.
Common Mistakes
- Using zero or a negative length. Left with 0 returns an empty string; a negative length raises run-time error 5 (Invalid procedure call or argument). Validate the number before calling Left.
- Passing a number instead of text. Left expects a string. A numeric cell value is coerced, but dates and formatted numbers can produce unexpected text, so convert deliberately with CStr when needed.
- Confusing VBA Left with the worksheet LEFT function. In a worksheet formula you write =LEFT(A1,5). In VBA you write Left(Range("A1").Value, 5). Mixing the two syntaxes is a frequent source of compile errors.
- Forgetting the parentheses. Left is a function and needs its arguments in parentheses; leaving them off causes a syntax error.
- Assuming Left counts bytes. It counts characters, so accented letters and most Unicode text are handled correctly. Only the separate LeftB function works on bytes, and it is rarely needed.
- Losing the macro. Saving as .xlsx silently discards VBA. Always use .xlsm for macro workbooks.
Tips
- Use Len first when the length is unknown:
Left(txt, Len(txt) - 3)strips a fixed suffix safely. - Wrap calls in a small helper function if you slice the same column repeatedly; it keeps the main procedure readable.
- Test on a copy of the workbook before running a loop over live data, since a wrong length can overwrite good values.
- Use Option Explicit at the top of the module so undeclared variables are flagged, which catches many Left-related typos early.
- Remember that Left is not case-sensitive in the sense that it does not change case; it simply returns the characters exactly as they appear.
Troubleshooting
- Compile error: Expected: ) — a parenthesis is missing or misplaced. Count the opening and closing brackets on the line.
- Run-time error 5 — the length argument is negative. Check any calculation that feeds Left, especially InStr-based formulas.
- Nothing appears in the cell — the source cell may be empty, or the result was written to the wrong sheet. Confirm the sheet name in the Range reference.
- The macro disappeared after saving — the file was saved as .xlsx. Re-save as .xlsm and re-add the code.
- Macros are blocked — check the Trust Center settings in Excel and enable macros for the file location, or unblock the file in its properties. Only enable macros in files you trust.
- Unexpected characters at the start — the cell may contain leading spaces. Trim first with
Left(Trim(txt), n).
Frequently Asked Questions
What does the VBA Left function do?
It returns the first n characters of a string. Left("Hello", 2) returns "He".
What happens if the length is longer than the string?
Left returns the entire string without an error. Only a negative length causes a run-time error.
Can I use Left on a range of cells at once?
Not directly. Loop through the cells, or use a worksheet formula with =LEFT(A1,5) and fill down.
Is VBA Left the same as the worksheet LEFT function?
The logic is the same, but the syntax differs: worksheet formulas use =LEFT(A1,5), while VBA uses Left(Range("A1").Value, 5).
Does Left work with numbers?
It converts them to text first, so Left(12345, 2) returns "12" as a string, not a number.
Why do I get an error when the length comes from InStr?
If the delimiter is not found, InStr returns 0, so the length becomes -1. Check for zero before subtracting.