Text function

REPLACEB function

Replaces part of a text by byte position and byte length, counting CJK characters as 2 bytes when a DBCS language is the default.

Available in All versions of Excel, including Microsoft 365, Excel for Mac and Excel for the web

In every Excel versionWorks in Google Sheets

Syntax

REPLACEB(old_text, start_num, num_bytes, new_text)
ArgumentWhat it means
old_textRequiredThe original text or cell.
start_numRequiredByte position where replacement starts.
num_bytesRequiredHow many bytes to remove.
new_textRequiredThe text to insert.

Returns: The changed text.

How REPLACEB works

REPLACEB is the byte-based form of REPLACE. It is meant for text in Japanese, Chinese or Korean when positions are defined in bytes - for example in fixed-width export files whose layout says "bytes 7-10 hold the ward name". In the legacy double-byte encodings kanji, kana, hangul and full-width characters take 2 bytes, while Latin letters, digits and half-width katakana take 1.

Byte counting only happens when a double-byte (DBCS) language - Japanese, Chinese (Simplified or Traditional) or Korean - is set as the default editing language under File > Options > Language. With any other default language every character counts as 1 byte and the B version behaves exactly like the normal one.

The replacement text itself can be any length; only the part being removed is measured in bytes. Make sure start_num and num_bytes land on whole characters.

Examples

1Change the ward in an address (Japanese default language)

B2fx=REPLACEB(A2,7,4,"新宿区")
AB
1Address
2東京都港区東京都新宿区

Result: 東京都新宿区

東京都 occupies bytes 1-6, so 港区 is bytes 7-10. Those four bytes are replaced by 新宿区.

2Mixed single- and double-byte text (Japanese default language)

B2fx=REPLACEB(A2,4,4,"大阪")
AB
1Branch ID
2ID:東京ID:大阪

Result: ID:大阪

ID: is 3 single bytes; the city starts at byte 4 and fills 4 bytes, which are replaced by 大阪.

3Same address with an English default language

B2fx=REPLACEB(A2,4,2,"新宿区")
AB
1Address
2東京都港区東京都新宿区

Result: 東京都新宿区

Here each character counts as 1 byte, so the ward is at positions 4-5 - exactly what REPLACE would use.

⬇Practice workbookExamples with live formulas + practice sheets · .xlsx · 15 KB

Common errors and how to fix them

ErrorWhy it happensFix
#VALUE!start_num is less than 1 or num_bytes is negative.Correct the byte positions.

Tips

  • Find byte positions with FINDB and byte lengths with LENB so they always agree with REPLACEB.
  • A workbook using REPLACEB can give different results on PCs with different default languages; document the setting it expects.

Google Sheets

REPLACEB also exists in Google Sheets and works the same way for everyday use, so the examples above work there too.

REPLACEB in other languages

In a non-English version of Excel the function has a local name. Excel translates formulas automatically when a file is opened in another language.

LanguageFunction name
FrenchREMPLACERB
GermanERSETZENB
PolishZASTĄP.B
RussianЗАМЕНИТЬБ
SpanishREEMPLAZARB