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?
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.

C Moore

Well-known Member
Joined
Jan 17, 2014
Messages
619
Office Version
  1. 365
Platform
  1. Windows
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
17,913
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
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)
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,489
Messages
5,837,634
Members
430,506
Latest member
TonyIbbs

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top