Return substring

This tutorial shows how to extract a substring with a nominated start and end position using an Excel formula or VBA

Example: Return substring

Return substring

METHOD 1. Return substring using Excel formula

EXCEL

=MID(B5,C5,D5-C5+1)
This formula uses the Excel MID function with the number of characters to return being calculated between the start and end position, plus one, to return a substring.

METHOD 1. Return substring using VBA

VBA

Sub Return_substring()
'declare variables
Dim ws As Worksheet
Set ws = Worksheets("Analysis")
'get the substring

ws.Range("E5") = Mid(ws.Range("B5"), ws.Range("C5"), ws.Range("D5") - ws.Range("C5") + 1)

End Sub

Explanation about how to return substring

EXPLANATION

EXPLANATION

This tutorial shows how to extract a substring through the use of an Excel formula or VBA.
The Excel and VBA methods both use the MID function with the number of characters to return being calculated between the start and end position, plus one, to return a substring.
FORMULA
=MID(text, start_num, end_num-start_num+1)
ARGUMENTS
text: The string from which to extract the characters.
start_num: The position of the first character in the string that is to be extracted.
end_num: The position of the last character in the string that is to be extracted.

RELATED TOPICS

Related Topic Description Related Topic and Description
How to return the first word from a string using Excel and VBA
How to return the last word from a string using Excel and VBA

RELATED FUNCTIONS

Related Functions Description Related Functions and Description
The Excel MID function returns the specified number of characters from a selected string, starting at a specified position