How to Use the REPLACE Function in Google Sheets

The REPLACE function in Google Sheets helps you replace part of a text string, starting from a specified position and for a specified length, with a different text string.

Assume cell A1 contains the string "Apple 24, Mango 50, Total 74". The following formula would return "Apple 24, Mango 50: Total 74":

=REPLACE(A1, 20, 1, ":")

One challenge you may encounter when using this function is determining the position of the string to replace. Let’s explore how to tackle this. But before we dive further, let’s first understand the arguments in this function.

Syntax of the REPLACE Function in Google Sheets

Here is the syntax:

REPLACE(text, position, length, new_text)
  • text – The text in which you want to make the replacement.
  • position – The position (in number of characters) where the replacement will begin. The first character is numbered 1.
  • length – The number of characters in the text to be replaced.
  • new_text – The text that will replace the specified characters.

As a side note, the SUBSTITUTE function matches a specific text and replaces all its occurrences in the string. In contrast, the REPLACE function doesn’t search for text; instead, it replaces characters at a specified position and length.

Using the REPLACE Function and Overcoming Challenges

Let’s start with a basic example.

Assume the text in cell A1 is "Apple or Mango", and you want to replace "or" with "and".

To find the position of "or", use the following formula:

=LEN("Apple ") + 1

This formula would return 7.

Now, use the following REPLACE formula to replace "or" with "and":

=REPLACE(A1, 7, 2, "and")

In short, copy the string up to the substring you want to replace, calculate its length, and add 1. This will give you the starting position for the replacement.

REPLACE Function with Numbers and Dates

The REPLACE function can also handle numbers and dates, but the output will be text. You can convert the result back to a number or date using additional functions.

Example 1: Replacing Part of a Date

If the value in cell A1 is the date 25/12/2024, you can replace 2024 with 2025 using this formula:

=REPLACE(A1, 7, 4, "2025")

The result will be text, but you can format it back into a date using DATEVALUE and TO_DATE in combination with REPLACE:

=TO_DATE(DATEVALUE(REPLACE(A1, 7, 4, "2025")))

Example 2: Replacing Part of a Number

If the value in cell A1 is the number 1250 and you want to change it to 1240, use the following formula:

=VALUE(REPLACE(A1, 3, 1, 4))

Here, the VALUE function is used to convert the text result back to a number.

Resources

Prashanth KV
Prashanth KV
Your Trusted Google Sheets and Excel Guide Prashanth KV brings a wealth of experience in Google Sheets and Excel, cultivated through years of work with multinational corporations in Mumbai and Dubai. As a recognized Google Product Expert in Docs Editors, Prashanth shares his expertise through insightful blogging since 2012. Explore his blog for practical tips and guidance on maximizing your spreadsheet skills.

Highlight Upcoming Birthdays in Google Sheets

When highlighting upcoming birthdays in Google Sheets, one important aspect is ignoring the year...

Compare Two Tables for Differences in Excel

To compare two tables with similar data for differences, you can use the XLOOKUP...

Calculate Weighted Average in Pivot Table in Google Sheets

You can calculate a weighted average in a Pivot Table using a custom formula...

Summarize Data and Keep the Last Record in Google Sheets

In Google Sheets, we can summarize data (like sum, average, min, max, count) using...

More like this

Highlight Upcoming Birthdays in Google Sheets

When highlighting upcoming birthdays in Google Sheets, one important aspect is ignoring the year...

Calculate Weighted Average in Pivot Table in Google Sheets

You can calculate a weighted average in a Pivot Table using a custom formula...

Summarize Data and Keep the Last Record in Google Sheets

In Google Sheets, we can summarize data (like sum, average, min, max, count) using...

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.