MIDB Function in Excel

Part 1: Introduce

  • Definition: The MIDB function in Microsoft Excel extracts a specific number of characters from a text string, considering double-byte character languages, starting at a specified position.

  • Purpose: The MIDB function is handy when working with languages that use double-byte characters, such as Chinese, Japanese, or Korean, where each character occupies two bytes of storage.

  • Syntax & Arguments:

    • MIDB(text, start_num, num_chars)
      • text: The text string from which you want to extract characters.
      • start_num: The starting position from where you want to remove characters.
      • num_chars: The number of characters you want to extract from the text.
  • Return value: The MIDB function returns the specified number of characters from the text string, starting at the fixed position, considering double-byte character languages.

  • Remarks:

    • If the start_num is less than 1, the function returns an empty string (“”).
    • If the start_num is greater than the length of the text, the function returns an empty string (“”).
    • If the num_chars is negative, the function returns an empty string (“”).
    • If the num_chars exceeds the number of characters available from the start_num position to the end of the text, the function returns all the remaining characters.

Part 2: Examples

Example 1: Extracting First Name from Full Name Suppose you have a table with full names in Column A and want to extract only the first name from each full name. Here’s how you can do it:

AB
1Full NameFirst Name
2John Smith=MIDB(A2, 1, FIND(” “, A2)-1)
3Jane Doe
4Tom Johnson
  • In cell B2, use the formula =MIDB(A2, 1, FIND(" ", A2)-1) to extract the first name from the full name.
  • The FIND function is nested within the MIDB function to determine the position of the space character (” “) in the full name, and the MIDB function then extracts the characters from the beginning until one character before the space.
  • The result in cell B2 will be “John” for the full name “John Smith”.

Example 2: Extracting the Last Word from Sentence Suppose you have a list of sentences in Column A and want to extract the last word from each sentence. Here’s how you can do it:

AB
1SentenceLast Word
2I love using Microsoft Excel for data analysis.=MIDB(A2, FIND(“@”, SUBSTITUTE(A2, ” “, “@”, LEN(A2) – LEN(SUBSTITUTE(A2, ” “, “”)))) + 1, LEN(A2))
3This is a sample sentence for demonstration.
4Extracting the last word can be useful.
  • In cell B2, use the formula =MIDB(A2, FIND("@", SUBSTITUTE(A2, " ", "@", LEN(A2) - LEN(SUBSTITUTE(A2, " ", "")))) + 1, LEN(A2)) to extract the last word from the sentence.
  • The SUBSTITUTE function replaces the last space with the “@” symbol, and then the FIND function finds the position of the “@” mark. The MIDB function extracts the characters from one place after the “@” symbol until the end of the text.
  • The result in cell B2 will be “analysis” for the sentence “I love using Microsoft Excel for data analysis.”

Example 3: Extracting Nth Character from Text Suppose you have a list of words in Column A and want to extract the third character from each word. Here’s how you can do it:

AB
1WordNth Character
2Excel=MIDB(A2, 3, 1)
3Data
4Analysis
  • In cell B2, use the formula =MIDB(A2, 3, 1) to extract the third character from the word.
  • The MIDB function extracts a single character starting from the third position in the word.
  • The result in cell B2 will be “c” for the word “Excel”.

 

Example 4: Extracting Company Code Suppose you have a list of product codes in Column A, and each code consists of a company code followed by a product number. You want to extract only the company code. Here’s how you can do it:

AB
1Product CodeCompany Code
2ABC-123=MIDB(A2, 1, FIND(“-“, A2)-1)
3XYZ-456
4PQR-789
  • In cell B2, use the formula =MIDB(A2, 1, FIND("-", A2)-1) to extract the company code from the product code.
  • The FIND function is nested within the MIDB function to determine the position of the hyphen (“-“) in the product code, and the MIDB function then extracts the characters from the beginning until one character before the hyphen.
  • The result in cell B2 will be “ABC” for the product code “ABC-123”.

Example 5: Extracting Domain Name Suppose you have a list of email addresses in Column A and want to extract only the domain name from each email address. Here’s how you can do it:

AB
1Email AddressDomain Name
2[email protected]=MIDB(A2, FIND(“@”, A2)+1, LEN(A2))
3[email protected]
4[email protected]
  • In cell B2, use the formula =MIDB(A2, FIND("@", A2)+1, LEN(A2)) to extract the domain name from the email address.
  • The FIND function finds the position of the “@” symbol in the email address, and the MIDB function then extracts the characters from one place after the “@” symbol until the end of the text.
  • The result in cell B2 will be “example.com” for the email address “[email protected]“.

Example 6: Extracting Product Category Suppose you have a list of product codes in Column A, and each code consists of a product category followed by a product number. You want to extract only the product category. Here’s how you can do it:

AB
1Product CodeProduct Category
2C00123=MIDB(A2, 2, 3)
3E00234
4S00345
  • In cell B2, use the formula =MIDB(A2, 2, 3) to extract the product category from the product code.
  • The MIDB function extracts three characters starting from the second position in the product code.
  • The result in cell B2 will be “001” for the product code “C00123”.

Example 7: Extracting Month from Date Suppose you have a list of dates in Column A, and you want to extract the month from each date. Here’s how you can do it:

AB
1DateMonth
201/15/2022=MIDB(TEXT(A2, “mm/dd/yyyy”), 1, 2)
303/22/2022
411/07/2022
  • In cell B2, use the formula =MIDB(TEXT(A2, "mm/dd/yyyy"), 1, 2) to extract the month from the date.
  • The TEXT function converts the date to a text string in the desired format (“mm/dd/yyyy”), and the MIDB function then extracts the first two characters, representing the month.
  • The result in cell B2 will be “01” for the date “01/15/2022”.

Example 8: Extracting Employee ID Suppose you have a list of employee IDs in Column A, and each ID consists of a department code followed by a unique number. You want to extract only the unique number. Here’s how you can do it:

AB
1Employee IDUnique Number
2D00123=MIDB(A2, 2, LEN(A2)-1)
3M00234
4S00345
  • In cell B2, use the formula =MIDB(A2, 2, LEN(A2)-1) to extract the unique number from the employee ID.
  • The MIDB function extracts the characters from the second position in the employee ID until the end of the text.
  • The result in cell B2 will be “00123” for the employee ID “D00123”.

Example 9: Extracting Text Between Delimiters Suppose you have a list of strings in Column A, and each string is enclosed between two special characters. You want to extract the text between those special characters. Here’s how you can do it:

AB
1StringExtracted Text
2[ABC123]=MIDB(A2, 2, FIND(“]”, A2)-2)
3{XYZ456}
4(PQR789)
  • In cell B2, use the formula =MIDB(A2, 2, FIND("]", A2)-2) to extract the text between the square brackets.
  • The FIND function is nested within the MIDB function to determine the position of the closing bracket (“]”) in the string, and the MIDB function then extracts the characters starting from the second position until two characters before the closing bracket.
  • The result in cell B2 will be “ABC123” for the string “[ABC123]”.

Example 10: Extracting Substring Using Variable Length Suppose you have a list of strings in Column A and want to extract a substring from each line starting from a variable position and with a variable length. Here’s how you can do it:

ABC
1StringStartLength
2Hello World75
3Good Morning37
4Excel Power63
  • In cell C2, use the formula =MIDB(A2, B2, C2) to extract a substring from the string based on the start position and length specified in columns B and C, respectively.
  • The MIDB function extracts the specified number of characters from the start position in the string.
  • The result in cell C2 will be “World” for the string “Hello World” with a start position of 7 and length of 5.

Part 3: Tips and Tricks

  • The MIDB function is handy when working with languages that use double-byte characters, where each character occupies two bytes of storage.
  • Ensure that the start_num and num_chars arguments are specified correctly to extract the desired portion of the text.
  • To remove a range of characters, you can modify the start_num and num_chars arguments accordingly.
  • Combine the MIDB function with FIND, SUBSTITUTE, or LEN to perform more complex text manipulations.