How to Insert Text Based on the First Three Characters Using VBA in Excel
Question details
The user wants to automate data entry by checking the first three characters of a code in one column and inserting a corresponding full name into another column.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Automating repeated text entries by matching prefix codes like 'CFC' in column K to full names like 'Canadian Air Force' in column H.
- Observed behavior
- Manual data entry is inefficient; the goal is to successfully use VBA or formulas to read the first three characters and output the corresponding text automatically.
Ensure your workbook is saved as an Excel Macro-Enabled Workbook (.xlsm) before writing your script to prevent losing your VBA code upon closing.
Use a VBA Macro with the LEFT Function and SELECT CASE
Create a VBA script that loops through your target column, extracts the first three characters using the LEFT function, and outputs the matching string.
This method is ideal for processing large datasets automatically. By combining a 'For Each' loop with a 'Select Case' statement, you can easily map multiple three-letter codes (like UAF, RRR, CFC) to their respective full names.
Press 'Alt + F11' on your keyboard to open the Visual Basic for Applications (VBA) editor.
Click on 'Insert' in the top menu bar and select 'Module' to create a blank script window.
Write a macro using the Left function. For example: `Select Case Left(cell.Value, 3)` followed by `Case "CFC": cell.Offset(0, -3).Value = "Canadian Air Force"`.
Close the VBA editor, return to your worksheet, and press 'Alt + F8'. Select your newly created macro and click 'Run' to populate the target cells.

Use an Excel Formula Combining LEFT and VLOOKUP
If you prefer not to use VBA macros, you can achieve the exact same result dynamically using built-in lookup formulas.
Automate Text Insertion in WPS Spreadsheet
WPS Spreadsheet provides robust formula capabilities and macro support to help you automate repetitive tasks like matching text prefixes instantly.
- 1. Open Your File in WPS Spreadsheet: Launch WPS Office and open your dataset containing the three-letter codes.
- 2. Apply the Left Function Formula: Select your target cell and type your VLOOKUP and LEFT combination formula to extract and match the first three characters dynamically.
- 3. Access Developer Tools for Macros: If you prefer scripting, navigate to the 'Developer' tab to access the Macro editor and write your automation code.
- 4. Run Your Automation: Execute your macro or drag the formula down using the fill handle to process the entire column instantly.

Frequently Asked Questions
Why is my VBA Left function returning an error on blank cells?
This usually happens because the script is trying to evaluate a cell that contains no text. You can fix this by wrapping your code in an 'If not IsEmpty(cell) Then' statement, or by adding 'On Error Resume Next' at the beginning of your script.
Can I check more than three characters using this VBA method?
Yes. You can easily modify the length parameter in the Left function. For example, changing `Left(cell.Value, 3)` to `Left(cell.Value, 4)` will command Excel to evaluate the first four characters instead.
How do I make the macro run automatically as soon as I type a code?
You can automate the trigger by using the Worksheet_Change event in VBA. Place your script inside this event handler targeting your specific input column, and the macro will instantly output the full name whenever a new code is entered.
Is it better to use CHOOSE and MATCH instead of VLOOKUP?
For smaller lists with only a few fixed codes, a CHOOSE and MATCH combination is acceptable and self-contained. However, for longer lists or codes that update frequently, VLOOKUP or XLOOKUP referencing a separate table is much easier to manage.




