Splitting text and extracting an element

aa2000

Board Regular
Joined
Aug 3, 2011
Messages
87
Hi all

I have a worksheet in which certain cells have data in the following format:

GROUP-ABCD-1.txt

Is it possible to separate text like the above with "-" as the delimiter and then set a string as the text between the 2 "-".

So if s is the string, then for the above example s would be ABCD.

Can this be done?

Cheers
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Whilst that does work, this code is supposed to be part of a larger macro, so can this be done using VB?

Sorry for the confusion, and thanks for your help
 
Upvote 0
Try like this

Code:
Sub test()
Const s As String = "GROUP-ABCD-1.txt"
Dim a As String
a = Mid(s, InStr(s, "-") + 1, Len(s) - InStrRev(s, "-") - 1)
MsgBox a
End Sub
 
Upvote 0
Thanks Peter, this is what I was looking for.

I have modified the code as below, but MsgBox is blank. Any ideas why?

Code:
Sub test()
Dim s As String
Dim a As String
s = Cells(1, 1).Value
a = Mid(s, InStr(s, "-") + 1, Len(s) - InStrRev(s, "-") - 1)
MsgBox a
End Sub

The value in Cells(1, 1) is GROUP-ABCD-1 since the .txt part does not need to be there.

Cheers
 
Upvote 0
Try

Code:
Sub test()
Dim s As String
Dim a As String
s = Cells(1, 1).Value
a = Mid(s, InStr(s, "-") + 1, InStrRev(s, "-") - InStr(s, "-") - 1)
MsgBox a
End Sub
 
Upvote 0
Another way to consider:
Code:
Sub ExtractText()
    Dim s As String, a As String
    
    s = "GROUP-ABCD-1.txt"
    a = Split(s, "-")(1)
    MsgBox a
End Sub
If there could be extra unwanted spaces in there then
Code:
a = Trim(Split(s, "-")(1))
 
Upvote 0
Thank you both for help so far.

Now I am using VOG's code currently for cells with data in the form "Group-ABCD-1, but how can I modify the code to work for data in the form "Group-ABCD-10-1" ?

Cheers
 
Upvote 0
Thank you both for help so far.

Now I am using VOG's code currently for cells with data in the form "Group-ABCD-1, but how can I modify the code to work for data in the form "Group-ABCD-10-1" ?

Cheers
Try my code? (Assuming you still want "ABCD" returned from that string)
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,291
Members
452,902
Latest member
Knuddeluff

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