REPLACE, REPLACEB functions

This article describes the formula syntax and usage of the REPLACE and REPLACEB function (function: A prewritten formula that takes a value or values, performs an operation, and returns a value or values. Use functions to simplify and shorten formulas on a worksheet, especially those that perform lengthy or complex calculations.) in Microsoft Excel.

Description

REPLACE replaces part of a text string, based on the number of characters you specify, with a different text string.

REPLACEB replaces part of a text string, based on the number of bytes you specify, with a different text string.

 Important   REPLACE is intended for use with languages that use the single-byte character set (SBCS), whereas REPLACEB is intended for use with languages that use the double-byte character set (DBCS). The default language setting on your computer affects the return value in the following way:

  • REPLACE always counts each character, whether single-byte or double-byte, as 1, no matter what the default language setting is.
  • REPLACEB counts each double-byte character as 2 when you have enabled the editing of a language that supports DBCS and then set it as the default language. Otherwise, REPLACEB counts each character as 1.

The languages that support DBCS include Japanese, Chinese (Simplified), Chinese (Traditional), and Korean.

Syntax

REPLACE(old_text, start_num, num_chars, new_text)
REPLACEB(old_text, start_num, num_bytes, new_text)

The REPLACE and REPLACEB function syntax has the following arguments (argument: A value that provides information to an action, an event, a method, a property, a function, or a procedure.):

  • Old_text    Required. Text in which you want to replace some characters.
  • Start_num    Required. The position of the character in old_text that you want to replace with new_text.
  • Num_chars    Required. The number of characters in old_text that you want REPLACE to replace with new_text.
  • Num_bytes    Required. The number of bytes in old_text that you want REPLACEB to replace with new_text.
  • New_text    Required. The text that will replace characters in old_text.

Example

Example 1: REPLACE

The example may be easier to understand if you copy it to a blank worksheet.

ShowHow do I copy an example?

  1. Select the example in this article. If you are copying the example in Excel Online, copy and paste one cell at a time.
    Important: Do not select the row or column headers.

Selecting an example from Help

Selecting an example from Help

  1. Press CTRL+C.
  2. Create a blank workbook or worksheet.
  3. In the worksheet, select cell A1, and press CTRL+V. If you are working in Excel Online, repeat copying and pasting for each cell in the example.
    Important: For the example to work properly, you must paste it into cell A1 of the worksheet.
  4. To switch between viewing the results and viewing the formulas that return the results, press CTRL+` (grave accent), or on the Formulas tab, in the Formula Auditing group, click the Show Formulas button.

After you copy the example to a blank worksheet, you can adapt it to suit your needs.

 
1
2
3
4
5

6

7

8
A B
Data
abcdefghijk
2009
123456
Formula Description (Result)
=REPLACE(A2,6,5,"*") Replaces five characters, starting with the sixth character (abcde*k)
=REPLACE(A3,3,2,"10") Replaces the last two digits of 2009 with 10 (2010)
=REPLACE(A4,1,3,"@") Replaces the first three characters with @ (@456)

Example 1: REPLACE

Example 2: REPLACEB (with your computer set to a default language that supports DBCS)

In the following example:

  • REPLACEB returns "Tokyo to Shibuya Tokyo to Shibuya Tokyo to Shibuya Tokyo to Shibuya " because each character is counted as 2; the second argument specifies a starting point at the fourth byte, which is the second character, and the third argument specifies a length of 4 bytes, which is two characters.
  • REPLACE returns "Tokyo to Shibuya Tokyo to Shibuya Tokyo to Shibuya Tokyo to Shibuya Tokyo to Shibuya " because each character is counted as 1; the second argument specifies a starting point at the fourth character, and the third argument specifies a length of 4 characters. REPLACE returns "Tokyo to Shibuya Tokyo to Shibuya Tokyo to Shibuya Tokyo to Shibuya Tokyo to Shibuya " no matter what the default language setting is on your computer.

=REPLACEB("Tokyo to Shibuya Tokyo to Shibuya Tokyo to Shibuya Tokyo to Shibuya ",4,4,"Tokyo to Shibuya Tokyo to Shibuya ") equals "Tokyo to Shibuya Tokyo to Shibuya Tokyo to Shibuya Tokyo to Shibuya "

=REPLACE("Tokyo to Shibuya Tokyo to Shibuya Tokyo to Shibuya Tokyo to Shibuya ",4,4,"Tokyo to Shibuya Tokyo to Shibuya ") equals "Tokyo to Shibuya Tokyo to Shibuya Tokyo to Shibuya Tokyo to Shibuya Tokyo to Shibuya "

 
 
Applies to:
Excel 2010, Excel Web App, SharePoint Online for enterprises, SharePoint Online for professionals and small businesses