Text function

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)
ArgumentWhat it means
textRequiredThe text or cell.
start_numRequiredByte position of the first character to return.
num_bytesRequiredHow 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)

B2fx=MIDB(A2,5,4)
AB
1Address
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)

B2fx=MIDB(A2,4,8)
AB
1Record
2A01東京支店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

B2fx=MIDB(A2,2,3)
AB
1Address
2東京都港区京都港

Result: 京都港

Without a DBCS default language bytes equal characters, so MIDB acts like MID and returns characters 2 to 4.

⬇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 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.

LanguageFunction name
GermanTEILB
PolishFRAGMENT.TEKSTU.B
RussianПСТРБ
SpanishEXTRAEB