Extracting portions of a string separated with dash

mandukes

Forum Rules
Joined
May 25, 2013
Messages
90
Hi!,
My variable has the string in the following format

Code:
key = ABCD8E-FG6HI-7JKLMN  (alpha numeric) separated with dash

Note that the length of the key is not fixed.

Required output

Code:
partA = ABCD8E
PartB = FG6HI
PartC =7JKLMN


Thank you!
 
Last edited:

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Consider using the Split function.

Code:
    [COLOR=darkblue]Dim[/COLOR] strKey [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR], vKeys [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Variant[/COLOR]
    
    strKey = "ABCD8E-FG6HI-7JKLMN"  [COLOR=green]'(alpha numeric) separated with dash[/COLOR]
    vKeys = Split(strKey, "-")
    
    MsgBox vKeys(0) & vbLf & vKeys(1) & vbLf & vKeys(2)


    partA = vKeys(0)
    PartB = vKeys(1)
    PartC = vKeys(2)
 
Last edited:
Upvote 0
part A is =TRIM(LEFT(SUBSTITUTE(A1,"-",REPT(" ",255)),255))

part B is =TRIM(MID(SUBSTITUTE(A1,"-",REPT(" ",255)),255,255))

part C is =TRIM(RIGHT(SUBSTITUTE(A1,"-",REPT(" ",255)),255))
 
Upvote 0
Consider using the Split function.

Code:
    [COLOR=darkblue]Dim[/COLOR] strKey [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR], vKeys [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Variant[/COLOR]
    
    strKey = "ABCD8E-FG6HI-7JKLMN"  [COLOR=green]'(alpha numeric) separated with dash[/COLOR]
    vKeys = Split(strKey, "-")
    
    MsgBox vKeys(0) & vbLf & vKeys(1) & vbLf & vKeys(2)


    partA = vKeys(0)
    PartB = vKeys(1)
    PartC = vKeys(2)


Thank you so much!
 
Upvote 0

Forum statistics

Threads
1,215,054
Messages
6,122,895
Members
449,097
Latest member
dbomb1414

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