Return text before a specific character
To return text before a specific character we can apply a combination of Excel LEFT and FIND functions
Example: Return text before a specific character
Dim ws As Worksheet
ws.Range("C5") = Left(ws.Range("B5"), (Application.WorksheetFunction.Find("/", ws.Range("B5"), 1) - 1))
End Sub
Worksheets: The Worksheets object represents all of the worksheets in a workbook, excluding chart sheets.
Range: The Range object is a representation of a single cell or a range of cells in a worksheet.
Worksheet Name: Have a worksheet named Analysis.
Text: If using the exact VBA code the string from which you want to extract text needs to be captured in cell ("B5").
ADJUSTABLE PARAMETERS
Output Range: Select the output range by changing the cell reference ("C5") in the VBA code to any cell in the worksheet, that doesn't conflict with the formula.
Text: Select the cell that captures the string from which you want to extract text by changing the cell reference ("B5") in the VBA code to any cell in the worksheet, that doesn't conflict with the formula.
Character: In this example we are returning all of the text before the "/" sing. You can change this in the VBA code to any character that is captured in the cell that captures the string from which you want to extract the text.
METHOD 2. Return text before a specific character using VBA with a formula function
VBA
Dim ws As Worksheet
ws.Range("C5").Formula = "=LEFT(B5,(FIND(""/"",B5,1)-1))"
End Sub
Worksheets: The Worksheets object represents all of the worksheets in a workbook, excluding chart sheets.
Range: The Range object is a representation of a single cell or a range of cells in a worksheet.
Worksheet Name: Have a worksheet named Analysis.
Text: If using the exact VBA code the string from which you want to extract text needs to be captured in cell ("B5").
ADJUSTABLE PARAMETERS
Output Range: Select the output range by changing the cell reference ("C5") in the VBA code to any cell in the worksheet, that doesn't conflict with the formula.
Text: Select the cell that captures the string from which you want to extract text by changing the cell reference ("B5") in the VBA code to any cell in the worksheet, that doesn't conflict with the formula.
Character: In this example we are returning all of the text before the "/" sing. You can change this in the VBA code to any character that is captured in the cell that captures the string from which you want to extract the text.
To return text before a specific character we can apply a combination of Excel LEFT and FIND functions.
=LEFT(string,(FIND("/",string,1)-1))
ARGUMENTS
string: The string that captures the text that you want to extract.