MIDB function
Returns characters from the middle of a text, with start and length measured in bytes (2 per CJK character 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
MIDB(text, start_num, num_bytes)| Argument | What it means | |
|---|---|---|
text | Required | The text or cell. |
start_num | Required | Byte position of the first character to return. |
num_bytes | Required | How many bytes to return. |
Returns: Text: the characters that fall into the given byte range.
How MIDB works
MIDB is the byte-based version of MID. It is used to read fixed-width records produced by Japanese, Chinese or Korean legacy systems, where each field occupies a set number of bytes: a kanji or other double-byte character takes 2 bytes and a Latin letter or digit 1. With MIDB you can use the byte positions from the file specification directly.
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.
As with LEFTB, a byte range that starts or ends in the middle of a double-byte character cannot return half a character; Excel adjusts the result (often with a space). Use byte positions that land on character boundaries.
Examples
1Middle characters of an address (Japanese default language)
| A | B | |
|---|---|---|
| 1 | Address | |
| 2 | 東京都港区 | 都港 |
Result: 都港
Bytes 1-4 are 東京. Starting at byte 5, four bytes cover 都 and 港. MID(A2,5,4) would instead return 区.
2Read a field from a fixed-width record (Japanese default language)
| A | B | |
|---|---|---|
| 1 | Record | |
| 2 | A01東京支店0150 | 東京支店 |
Result: 東京支店
The specification says the branch name occupies bytes 4-11. A01 is 3 bytes, then four kanji fill the 8 bytes.
3English default language
| A | B | |
|---|---|---|
| 1 | Address | |
| 2 | 東京都港区 | 京都港 |
Result: 京都港
Without a DBCS default language bytes equal characters, so MIDB acts like MID and returns characters 2 to 4.
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 positions. |
Tips
- Build a small table of field start bytes and lengths from the file specification and point MIDB at it, so one formula can be filled across all fields.
- Check each record with LENB first; a record that is not the expected byte length usually means an encoding problem in the import.
Google Sheets
MIDB also exists in Google Sheets and works the same way for everyday use, so the examples above work there too.
MIDB 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.