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

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
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,224,823
Messages
6,181,185
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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