Text functions
MIDB
Matches all 7 recorded answers
Recorded: 7 workbook cells. How we measure this · See the Rust code
Local names in 40 languages
| Language | Name in Excel |
|---|---|
| Arabic | MIDB |
| Bulgarian | MIDB |
| Czech | MIDB |
| Danish | MIDTB |
| German | MIDB |
| Greek | MIDB |
| English (Australia) | MIDB |
| English (United Kingdom) | MIDB |
| English (United States) | MIDB |
| Spanish | EXTRAEB |
| Estonian | MIDB |
| Finnish | POIMI.TEKSTIB |
| French | STXTB |
| Hebrew | MIDB |
| Croatian | MIDB |
| Hungarian | KÖZÉP2 |
| Indonesian | MIDB |
| Italian | MIDB |
| Japanese | MIDB |
| Korean | MIDB |
| Lithuanian | MIDB |
| Latvian | MIDB |
| Norwegian | MIDB |
| Dutch | DEELB |
| Polish | FRAGMENT.TEKSTU.B |
| Portuguese (Brazil) | MIDB |
| Portuguese (Portugal) | SEG.TEXTOB |
| Romanian | MIDB |
| Russian | ПСТРБ |
| Slovak | MIDB |
| Slovenian | MIDB |
| Serbian (Latin) | MIDB |
| Swedish | EXTEXTB |
| Thai | MIDB |
| Turkish | ORTAB işlevleri |
| Ukrainian | MIDB |
| Vietnamese | MIDB |
| Chinese (Simplified) | MIDB |
| Chinese (Hong Kong) | MIDB |
| Chinese (Traditional) | MIDB |
Names from Microsoft's support site.
What it does
MIDB(text, start_num, num_bytes)
MIDB(text, start_num, num_bytes) returns the run of text that begins at byte position
start_num and is num_bytes bytes long, where byte positions are counted from 1.
The whole content of this function is the word "byte", and Microsoft states precisely when it means anything:
The B functions count each double-byte character as 2 only when a DBCS language has been enabled for editing and set as the default language. Otherwise they count each character as 1.
So MIDB has two behaviours and which one you get is a property of the machine, not of the
formula. On a default-English installation MIDB(text, s, k) and MID(text, s, k) are the
same function. On an installation whose default language is Japanese, Chinese (Simplified or
Traditional), or Korean, every character in a double-byte range costs two positions.
The byte count is against the DBCS code page for the active language, not UTF-8 and not UTF-16. That distinction matters for anyone implementing this: a UTF-8 byte count would make a Latin-1 accented character cost two bytes and a CJK character cost three, and neither matches the DBCS rule.
Arguments
| Argument | Meaning |
|---|---|
text | The string to slice. Required. |
start_num | 1-based byte position of the first byte to take. Required. |
num_bytes | How many bytes to take. Required, and must not be negative. |
All three are required. The argument name is the only signature difference from MID; the
arity and positions are identical.
Try it
Quirks
Result and edge cases
Returns Text.
The boundary rules Microsoft documents for MID are stated on the same page for MIDB with
"characters" replaced by "bytes": a start_num past the end of text yields the empty string,
and a window running past the end yields the bytes to the end of text.
The genuinely MIDB-specific question — what happens when start_num or start_num + num_bytes lands inside a double-byte character rather than on its boundary — is not
answered by Microsoft's page. There are several defensible behaviours (return the half byte as
a replacement character, drop the partial character, round the boundary outward, round it
inward) and the Handbook does not know which one Excel implements. This is the single most
important open question about this function and it is not a corner case: any offset arithmetic
over mixed-width text hits it immediately.
Empty, missing and error arguments follow the shared call model; see Coercion and lifting.
Errors
As documented on Microsoft's MID, MIDB page:
| Error | Condition |
|---|---|
#VALUE! | start_num is less than 1. |
#VALUE! | num_bytes is negative. |
Non-numeric text in a numeric argument slot and incoming error values behave as the shared coercion rules describe. The Handbook has not verified any of this against Excel.
How to implement
Notes for implementers
Three things make this function harder than it looks.
- It is host-state dependent. The active default editing language is an input to the
function that does not appear in its argument list. Any implementation that wants to match
Excel needs that state threaded in, and any test vector for
MIDBis meaningless without recording it. - The code page is per-language. Shift-JIS, GBK, Big5 and EUC-KR assign different byte widths to different characters. "Two bytes for CJK" is a summary, not a specification.
- Partial-character windows are reachable and undefined here. Pick a behaviour, document it, and mark it as unverified — do not present a guess as the rule.
Rust code
Show the CalcFunc source of this function
Quoted from the public repository DnaCalc/DnaCalc, crates calc-func and calc-fml, at commit 9e1884369a67. Comments are the source's own. A kernel shared with other functions appears once, on its module page.
MIDB · 1 lines · calc-func/src/catalogue/text_functions.rs on GitHub12 midb_fn "MIDB" (3, 3) [V "text", V "start_num", V "num_bytes"] |s, a, sv| { single_byte(s)?; mid(s, a, true) }
Kernels shared with other functions
count_surrogate_pairscatalogue/text_functionssingle_bytecatalogue/text_functionsmidcatalogue/text_functionsstartcatalogue/text_functionstextported/text_kernelstext_from_strported/text_kernelsdate_integer_floorported/text_kernelsnonnegative_countported/text_kernelsmid_startported/text_kernelsunit_widthported/text_kernelscharacter_boundaryported/text_kernelstake_midported/text_kernelstruncatedported/text_kernelspositiveported/text_kernelspush_singleton_or_originalported/text_kernelspush_upperported/text_kernelsupperported/text_kernels
Argument and result plumbing
No implementation has been published with a Handbook test suite for this function.
Version differences
No comparison between specific Excel builds is recorded here yet. This does not establish that all builds behave alike.
Alternatives
Relationships
- MID is the character-counting twin. Microsoft documents the two on a single
page,
MID, MIDB; the Handbook publishes them as two entries because they are two functions with two different addressing units. LENBis the function you need to reason aboutMIDBoffsets at all, becauseMIDBpositions areLENBpositions, notLENpositions.
Named variants
No named variants are recorded yet.
Suggest a correction or contribute an implementation · For agents