MID Function (2024)

The MID function extracts a given number of characters from the middle of a supplied text string. MID takes three arguments, all of which are required. The first argument, text, is the text string to start with. The second argument, start_num, is the position of the first character to extract. The third argument, num_chars, is the number of characters to extract. If num_chars is greater than the number of characters available, MID returns all remaining characters.

MID function basics

To extract text with MID, just provide the text, the starting position, and the number of characters to extract. The formulas below show how to extract one, two, and three characters with MID:

=MID("apple",1,1) // returns "a"=MID("apple",1,2) // returns "ap"=MID("apple",1,3) // returns "app"

The formula below returns 3 characters starting at the 5th character:

=MID("The cat in the hat",5,3) // returns "cat"

This formula will extract 3 characters starting at character 16:

=MID("The cat in the hat",16,3) // returns "hat"

If num_chars is greater than the remaining characters, MID will all remaining characters:

=MID("apple",1,100) // returns "apple"

This can be useful as a way to simply certain formulas as explained below. MID can extract text from numbers, but the result is text:

=MID(12348,3,4) // returns "348" as text

Example 1 - extract date from serial number

In the example below, we use the MID function to extract a date in YYYYMM notation from a serial number with 14 characters. The formula in cell D5, copied down, is:

=MID(B5,5,6)

MID Function (1)

Note that the date is returned as text like "202105" which indicates the year and month but is not a valid date. If you want a valid date, you could use a formula like this:

=DATE(MID(B5,5,4),MID(B5,9,2),1)

Here we use the MID function twice: once to get the year, and once to get the month. The day is hard-coded as 1, and all values are returned to the DATE function, which returns a valid Excel date like 1-May-2021.

Example 2 - extract name from email address

A common challenge with MID is how to provide a variable end point. This can be done by combining MID with the FIND function. For example, in the worksheet below, the goal is to return the name portion of each email address. To perform this task, MID should start at the first character and end at the character before the "@" symbol. The formula in cell D5 looks like this:

=MID(B5,1,FIND("@",B5)-1)

MID Function (2)

As the formula is copied down, the FIND function returns the position of the "@" symbol as a number. We then subtract 1 and the result goes into the MID function as the num_chars argument.

Example 3 - extract domain from email address

A relatedchallenge with MID is how to provide a variable starting point. This too can be accomplished by combining MID with the FIND function. In the worksheet below, the goal is to return the domain portion of each email address. To perform this task, MID should start at the character after the "@" symbol and continue to the end of the text string. The formula in cell D5 looks like this:

=MID(B5,FIND("@",B5)+1,100)

MID Function (3)

In this formula, the starting position (start_num) is provided by FIND, which returns the location as a number of the "@" symbol. We then add 1 to the result from FIND to start at the next character. For num_chars, we hardcode 100. This is a simple hack. When num_chars exceeds the characters that remain in a text string, MID returns all remaining characters. We take advantage of this behavior by providing an arbitrarily large number that will work in all cases.

Example 4 - MID with IF

You can easily combine the MID function with the IF function to create "if cell contains" logic. In the example below, a formula is used to flag serial numbers that contain "2021". The formula in cell D5 is:

=IF(MID(B5,5,4)="2021","x","")

MID Function (4)

The MID function is configured to extract 4 characters beginning at character 5. The result is then compared to "2021" as the logical test inside the IF function. When the result is TRUE, IF returns "x". Otherwise, IF returns an empty string (""). The result is that serial numbers that contain "2021" are clearly identified.

Example 5 - MID with SEQUENCE

The MID function can also be used together with the SEQUENCE function to split a text string into an array of characters. This pattern shows up in more advanced formulas where it is necessary to iterate through a text string one character at a time. The generic version of the formula looks like this:

=MID(A1,SEQUENCE(1,LEN(A1)),1)

In brief, SEQUENCE spins up a numeric array based on the length of the text string in A1 and this array is delivered to the MID function as the start_num argument, with num_chars hardcoded as 1. The results is an array that contains all characters in the text string. See this page for a full explanation.

Related functions

Use theMID function to extract from the middle of a text string. Use theLEFT function to extract text from the left side of a text string andthe RIGHT functionto extract text starting from the right side of a text string. The LEN function returns the length of text as a count of characters.Use FIND or SEARCH to locate an unknown starting or ending position.

In the latest version of Excel, newer functions like TEXTBEFORE, TEXTAFTER, and TEXTSPLITgreatly simplify certain text operations and make some traditional formulas obsolete. Example here.

Notes

  • All three arguments are required.
  • The output from MID is always text.
  • MID can extractnumbers as well, but the result is text.
  • MID ignoresnumber formatting when extracting characters.
  • When num_chars is greaterthan the remaining text, MID returns all remaining characters
MID Function (2024)

FAQs

How many arguments are required for the mid function? ›

The MID Function in Excel extracts characters from the middle of a specific text. It uses the following arguments: Text (required argument), Start_num (required argument), and Num_chars (required argument).

How do I extract mid value in Excel? ›

Syntax
  1. MID(text,start_num,num_chars)
  2. Text is the text string containing the characters you want to extract.
  3. Start_num is the position of the first character you want to extract in text. The first character in text has start_num 1, and so on.
  4. Num_chars specifies the number of characters you want MID to return from text.

How do I extract text in the middle of a cell? ›

To extract a substring of a certain length from anywhere in the middle of a string, use the MID function. Here in this example, I enter =MID(A2,6,4) to extract four characters starting from the 6th character of the product code.

What does the function MID do in Excel? ›

Generally speaking, the MID function in Excel is designed to pull a substring from the middle of the original text string. Technically speaking, the MID function returns the specified number of characters starting at the position you specify.

How many arguments is too much for a function? ›

Typically, the suggestion is to stay way less than 7 arguments. If you have to pass that many arguments into a function, you would do better to create an object to pass the information in.

How many arguments must a function have? ›

Our functions should have the minimum number of arguments possible, if it have less than four argument, nice. Put a limit on the argument's number is not the ideal, we should strive to keep them as minimal as we can.

How do I extract text from a cell in Excel using mid? ›

MID formula using SEARCH formula to extract text from cell
  1. Identify cell containing text.
  2. Find the start number by nominating a first searchable character.
  3. Find the end number of the text being extracted by searching for the second searchable character.
Jan 12, 2023

How do I extract a range of values in Excel? ›

If you want to retrieve values from a range of cells, use the GetRange and GetRangeA1 methods instead. Methods that have the A1 suffix ( GetCellA1 and GetRangeA1) use a different coordinate system than those that do not ( GetCell and GetRange).

What is the mid and find formula in Excel? ›

MID() and FIND() are string functions. In Excel, string functions let you retrieve specific characters, also called a substring, from a source string. There are several string functions: LEFT(): Gets characters from the left side of the source string.

How do you use the mid formula in Excel with example? ›

For example, suppose we have a table of products from A2:A5: “Dolls,” “Barbie,” “Bottle,” and “Pen.” in the worksheet. We need to fetch the substring from the given substring. When we type, =MID(A2,2,4) returns “oll.” It will bring the substring from 2 characters and 4 letters from the 2nd position.

How do I extract specific data from a cell in Excel? ›

As we've already mentioned, manual extraction is the simplest extraction method. To use it, you need to open your dataset and select the data you want to extract. Then, copy the selected data and paste it into a new spreadsheet or another app where you need to work with that information.

How do you use mid function in sheets? ›

For example: =MID(A1, 2, 3) Assuming cell A1 contains the text "Hello", this formula will extract a substring starting from the 2nd character and return 3 characters. So the result would be "ell".

What is mid function in basic? ›

Visual Basic has a Mid function and a Mid statement. These elements both operate on a specified number of characters in a string, but the Mid function returns the characters while the Mid statement replaces the characters.

How do I use the mid function in Excel macro? ›

Step 1: Create a macro name first. Step 2: Declare a variable as “STRING.” Step 3: Now, assign a value to this variable through the MID function. Step 4: The first argument is String, i.e., from which value we want to extract.

How many arguments can be used in a function? ›

You can add as many arguments as you want, just separate them with a comma.

How many arguments can a function take? ›

Theoretically you can set these max stack size to 8192 bits. Each variable takes up 32 bits then you could pass 256 parameters. 8192/32 = 256. There is no maximum limit to pass parameters or arguments to a user defined function.

How do you know how many arguments a function has? ›

You can use arguments.length to count how many arguments the function was called with. If you instead want to count how many parameters a function is declared to accept, inspect that function's length property.

How many arguments does the Averageif function have? ›

The AVERAGEIF function has two required arguments and one optional argument. Range: the location where we can expect to find cells that meet the criteria.

Top Articles
Latest Posts
Article information

Author: Reed Wilderman

Last Updated:

Views: 6134

Rating: 4.1 / 5 (52 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Reed Wilderman

Birthday: 1992-06-14

Address: 998 Estell Village, Lake Oscarberg, SD 48713-6877

Phone: +21813267449721

Job: Technology Engineer

Hobby: Swimming, Do it yourself, Beekeeping, Lapidary, Cosplaying, Hiking, Graffiti

Introduction: My name is Reed Wilderman, I am a faithful, bright, lucky, adventurous, lively, rich, vast person who loves writing and wants to share my knowledge and understanding with you.