How to Swap Text Strings Using the REPLACE Function in Excel

When you are formatting a massive database of standardized identification codes in Microsoft Excel, you frequently need to overwrite a highly specific section of text based purely on its physical location. For example, if a company completely changes its product formatting rules, you might need to surgically remove the 3rd, 4th, and 5th characters of a 10-character serial number and swap them out for a brand new text string. Using the standard “Find and Replace” tool is impossible because it searches for specific words, not physical character positions. To overwrite text based exclusively on where it sits inside the cell, you must use the REPLACE function.

How the REPLACE Function Works

Unlike the SUBSTITUTE function (which scans the cell looking for a specific word to swap), the REPLACE function is completely blind to the actual text. It operates purely on spatial coordinates.

The syntax requires four distinct arguments: =REPLACE(old_text, start_num, num_chars, new_text)

  • old_text: The target cell containing the original, messy data.
  • start_num: The exact numerical position of the very first character you want to destroy.
  • num_chars: Exactly how many characters you want to destroy, counting left-to-right from the starting position.
  • new_text: The brand new string you want to inject into the empty hole.

Executing a Surgical Replacement

Imagine Cell A2 contains a legacy product serial number: PRD-2023-XYZ

Management dictates that the year “2023” must be permanently replaced with the word “BETA”. You know the year always begins exactly at character position number 5, and the year is always exactly 4 characters long.

Click into Cell B2 and type:

=REPLACE(A2, 5, 4, "BETA")

Excel executes the logic flawlessly: It looks at the text in A2. It counts over to the 5th character (the number ‘2’). It highlights exactly 4 characters (the entire block ‘2023’). It violently deletes that block and seamlessly injects the word “BETA”. The cell instantly outputs: PRD-BETA-XYZ.

Erasing Text Without Replacing It

The REPLACE function can also act as an incredibly precise deletion tool.

If you simply want to destroy the year “2023” and leave nothing behind (converting PRD-2023-XYZ into PRD--XYZ), you simply provide an empty text string (two double-quotes with absolutely nothing inside) as the final argument:

=REPLACE(A2, 5, 4, "")

This commands Excel to delete the 4 characters and replace them with a literal void, seamlessly pulling the remaining text together.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.