VBA & Macro Problems

Insert a RIGHT Formula or Extract Characters with Excel VBA

Khadija KhanKhadija Khan Dec 2, 2026 869 views

Question details

The user wants to extract the last four characters of a selected cell or use VBA to insert a RIGHT worksheet formula.

Insert a RIGHT Formula or Extract Characters with Excel VBA
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA editor

In Excel, open Developer > Visual Basic, then select Insert > Module.

2
Add the macro

Paste this code into the module: Sub ExtractLastFour() Selection.Value = Right(Selection.Value, 4) End Sub

3
Select and run

Return to the worksheet, select one cell containing the value, and run ExtractLastFour from Developer > Macros.

Replace the Selected Cell with Its Last Four Characters
The original value is replaced: The macro writes the extracted characters back into the selected cell. Values shorter than four characters remain unchanged.
Extract text in WPS Spreadsheet

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. 1. Open your spreadsheet: Open WPS Spreadsheet and use Open to select your workbook.
  2. 2. Choose a result cell: Select an empty cell next to the source value, such as B1 when the source is in A1.
  3. 3. Enter the formula: Type =RIGHT(A1,4) in B1 and press Enter.
  4. 4. Apply it to more rows: Drag the fill handle from B1 down the column to extract characters from additional source cells.
Extract the last four characters with a familiar RIGHT formulaOpen and edit Microsoft Excel XLSX and XLS filesUpdate the result automatically when the source cell changes
microsoft office alternative - wps office

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.