Function to Split a String

PRobinson87

New Member
Joined
Jan 22, 2014
Messages
8
I'm trying to split a String to get, in my case, product name. I am writing the function as a reference to the full string. The strings all start with "SKU-", the Product Name, then product description.

Ex.
SKU-123ABC-Doodad


<tbody>
</tbody>
I can't simply do text to columns because some Product Names contain dashes.

Ex.
SKU-321-ABC-Doodad
|--| |-------| |-------|

Essentially, what I need to do is replace everything before the first and after the last dash with "". The replace for the first is easy, it's finding the last dash and replacing everything after that is difficult.

Anyone have any experience with this?
 

Some videos you may like

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college

C Moore

Active Member
Joined
Jan 17, 2014
Messages
431
If the product names and product descriptions never have dashes, then Find("-",M15,5) could work. The formula would be =MID(cell,5,FIND("-",cell,5)-5)
 

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
16,732
Here's a UDF you can use with an example of use below.
Code:
Function ProdNum(S As String)
Dim First As Long, Last As Long
If S = "" Then
    ProdNum = ""
    Exit Function
End If
First = InStr(S, "-") + 1
Last = InStrRev(S, "-")
ProdNum = Mid(S, First, Last - First)
End Function
Excel Workbook
AB
1SKU-123ABC-Doodad123ABC
2SKU-321-ABC-Doodad321-ABC
Sheet1
 

István Hirsch

Well-known Member
Joined
May 16, 2013
Messages
1,634
Give this a try:

=MID(A1,5,FIND("ß",SUBSTITUTE(A1,"-","ß",LEN(A1)-LEN(SUBSTITUTE(A1,"-",""))))-5)
 

Watch MrExcel Video

Forum statistics

Threads
1,095,361
Messages
5,444,020
Members
405,260
Latest member
Khauff

This Week's Hot Topics

Top