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

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
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)
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,839
Messages
6,121,891
Members
449,058
Latest member
Guy Boot

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
Back
Top