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)| Argument | What it means | |
|---|---|---|
old_text | Required | The original text or cell. |
start_num | Required | Byte position where replacement starts. |
num_bytes | Required | How many bytes to remove. |
new_text | Required | The 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)
| A | B | |
|---|---|---|
| 1 | Address | |
| 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)
| A | B | |
|---|---|---|
| 1 | Branch ID | |
| 2 | ID:東京 | 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
| A | B | |
|---|---|---|
| 1 | Address | |
| 2 | 東京都港区 | 東京都新宿区 |
Result: 東京都新宿区
Here each character counts as 1 byte, so the ward is at positions 4-5 - exactly what REPLACE would use.
Common errors and how to fix them
| Error | Why it happens | Fix |
|---|---|---|
#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.
| Language | Function name |
|---|---|
| French | REMPLACERB |
| German | ERSETZENB |
| Polish | ZASTĄP.B |
| Russian | ЗАМЕНИТЬБ |
| Spanish | REEMPLAZARB |