Text functions
MID
Matches all 14 recorded answers
Recorded: 14 workbook cells. How we measure this · See the Rust code
Local names in 40 languages
| Language | Name in Excel |
|---|---|
| Arabic | MID |
| Bulgarian | MID |
| Czech | ČÁST |
| Danish | MIDT |
| German | MID |
| Greek | MID |
| English (Australia) | MID |
| English (United Kingdom) | MID |
| English (United States) | MID |
| Spanish | EXTRAE |
| Estonian | MID |
| Finnish | POIMI.TEKSTI |
| French | STXT |
| Hebrew | MID |
| Croatian | MID |
| Hungarian | KÖZÉP |
| Indonesian | MID |
| Italian | MID |
| Japanese | MID |
| Korean | MID |
| Lithuanian | MID |
| Latvian | MID |
| Norwegian | MID |
| Dutch | DEEL |
| Polish | FRAGMENT.TEKSTU |
| Portuguese (Brazil) | MID |
| Portuguese (Portugal) | SEG.TEXTO |
| Romanian | MID |
| Russian | ПСТР |
| Slovak | MID |
| Slovenian | MID |
| Serbian (Latin) | MID |
| Swedish | EXTEXT |
| Thai | MID |
| Turkish | PARÇAAL |
| Ukrainian | MID |
| Vietnamese | MID |
| Chinese (Simplified) | MID |
| Chinese (Hong Kong) | MID |
| Chinese (Traditional) | MID |
Names from Microsoft's support site.
What it does
MID(text, start_num, num_chars)
MID(text, start_num, num_chars) returns the contiguous run of num_chars characters of
text beginning at position start_num, where positions are counted from 1 at the first
character.
Written against the character positions of text, with n = LEN(text):
MID(text, s, k) = characters at positions s, s+1, …, min(s + k - 1, n)
and the empty string when s > n. Note what the clamp does and does not do: the end of the
window is clamped to the end of text, so asking for more characters than remain is legal and
simply yields the tail. The start is not clamped, so s < 1 is not "start at the
beginning" — it is an error (see Errors).
"Character" here means one UTF-16 code unit, the unit the Handbook's value universe uses for
worksheet text — the same unit LEN counts. A character outside the Basic Multilingual Plane
occupies two code units, so MID can in principle cut a surrogate pair in half. See
The value universe, "Text, exactly".
Arguments
| Argument | Meaning |
|---|---|
text | The string to slice. Required. |
start_num | 1-based position of the first character to take. Required. |
num_chars | How many characters to take. Required, and must not be negative. |
All three arguments are required — num_chars has no default, unlike the optional second
argument of LEFT and RIGHT. This is the position where the family is least uniform, and it
is a frequent source of confusion: RIGHT(a) is legal and means one character, MID(a, 2) is
not legal at all.
start_num and num_chars are numeric arguments, so ordinary to-number coercion applies to
whatever arrives in those slots (a logical, numeric text, a referenced number). The general
rules are in Coercion and lifting; nothing about them is
specific to MID. Fractional values are the interesting case, and the Handbook has not checked
how they are handled here — see "What has not been checked".
Try it
Quirks
Result and edge cases
Returns Text.
The boundary behaviours Microsoft documents on the MID page:
start_numgreater than the length oftext→ the empty string"", not an error.start_numwithintextbutstart_num + num_charsrunning past the end → the characters fromstart_numto the end oftext.num_charsof zero → the empty string.
Empty, missing and error arguments follow the shared call model rather than any MID-specific
rule; see The call pipeline and
Coercion and lifting. An error value arriving in any
argument propagates.
Errors
As documented on Microsoft's MID page:
| Error | Condition |
|---|---|
#VALUE! | start_num is less than 1. |
#VALUE! | num_chars is negative. |
Beyond those two, #VALUE! is also the expected result of a non-numeric-text value in a
numeric argument slot under the shared coercion rules, and any error value passed in
propagates. The Handbook has not verified the error surface against Excel.
How to implement
Notes for implementers
The start_num < 1 rule is the trap. It is tempting to saturate the window at both ends
symmetrically, which silently turns MID(t, 0, 3) into a two-character result instead of the
documented #VALUE!. Clamp the end; validate the start.
Counting in UTF-16 code units rather than Unicode scalar values is not an implementation convenience, it is the specified unit: an implementation that counts scalar values will disagree with Excel on any string containing an astral character, and will disagree in a way that only shows up on emoji and rarer CJK.
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.
MID · 1 lines · calc-func/src/catalogue/text_functions.rs on GitHub8 mid_fn "MID" (3, 3) [V "text", V "start_num", V "num_chars"] |s, a, sv| { mid(s, a, false) }
Kernels shared with other functions
count_surrogate_pairscatalogue/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.
Recorded variants
| Excel build | CPU | Recorded behaviour | Our Rust | Evidence |
|---|---|---|---|---|
| Compatibility Version 1, all Excel versions | Any | A surrogate pair counts as two characters. | Implemented; selected by the workbook's Compatibility Version. Code | https://support.microsoft.com/en-gb/excel/compatibility-versions · |
| Compatibility Version 2, Excel for Microsoft 365 | Any | A surrogate pair counts as one character. Characters combined from several code points still count separately. | Implemented; selected by the workbook's Compatibility Version. Code | https://support.microsoft.com/en-gb/excel/compatibility-versions · |
Alternatives
Relationships
LEFTandRIGHTare the two anchored slices;MIDis the free one.LEFT(t, k)isMID(t, 1, k), andRIGHT(t, k)isMID(t, LEN(t) - k + 1, k)for0 <= k <= LEN(t)— the second identity is exactly whyRIGHTis not simplyMIDwith a negative start.MIDBis the byte-counting sibling. Microsoft documents both on the same page and the Handbook splits them into two entries; see MIDB.REPLACEis the positional counterpart in the other direction:MIDreads out a window by position,REPLACEwrites over a window by position, with the same(start_num, num_chars)addressing.TEXTBEFORE,TEXTAFTERandTEXTSPLITare the modern delimiter-based way to do whatMID(…, SEARCH(…), …)formulas were traditionally written to do.MIDis not superseded — positional slicing is still its own job — but a formula that computesstart_numfrom aSEARCHresult is usually clearer written with the newer functions.
Named variants
No named variants are recorded yet.
Suggest a correction or contribute an implementation · For agents