Insert a RIGHT Formula or Extract Characters with Excel VBA
Question details
The user wants to extract the last four characters of a selected cell or use VBA to insert a RIGHT worksheet formula.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- A VBA macro should either replace a cell's value with its last four characters or write a formula that extracts them.
- Observed behavior
- The macro enters break mode when it passes a Range object to Range inappropriately; the desired result is either a shortened value or a working formula.
Decide whether to replace the selected value or keep the source in one cell and put a formula in another. The examples below assume a single selected cell; for the adjacent-cell formula, place the source immediately to its left.
Replace the Selected Cell with Its Last Four Characters
Use VBA's Right function when you want to overwrite a single cell with the extracted text.
In Excel, open Developer > Visual Basic, then select Insert > Module.
Paste this code into the module: Sub ExtractLastFour() Selection.Value = Right(Selection.Value, 4) End Sub
Return to the worksheet, select one cell containing the value, and run ExtractLastFour from Developer > Macros.

Insert a RIGHT Formula That Refers to Another Cell
Use FormulaR1C1 when the result should update as the source cell changes.
Insert a RIGHT Formula for Fixed Text
Use escaped quotation marks when the formula should extract characters from text supplied in the macro.
Extract the Last Four Characters in WPS Spreadsheet
Use a RIGHT formula in WPS Spreadsheet to extract characters from a source cell without writing a macro.
- 1. Open your spreadsheet: Open WPS Spreadsheet and use Open to select your workbook.
- 2. Choose a result cell: Select an empty cell next to the source value, such as B1 when the source is in A1.
- 3. Enter the formula: Type =RIGHT(A1,4) in B1 and press Enter.
- 4. Apply it to more rows: Drag the fill handle from B1 down the column to extract characters from additional source cells.

Frequently Asked Questions
Why does =RIGHT(RC,4) cause a circular reference?
In FormulaR1C1, RC refers to the cell containing the formula. Use a different source reference, such as RC[-1] for the cell immediately to the left.
How do I use an existing Range variable in VBA?
If cell is already a Range object, use cell.Value or cell.FormulaR1C1 directly instead of passing the object to Range(cell).
How do I put fixed text inside a VBA formula string?
Double the quotation marks around the text within the VBA string. For example, Selection.FormulaR1C1 = "=RIGHT(""Example"",4)" inserts a formula that returns mple.
Can I extract the last four characters without VBA?
Yes. In an empty cell, enter =RIGHT(A1,4), replacing A1 with the source cell. This works in both Excel and WPS Spreadsheet.




