Extract A String Between Two Characters

theaudioguy

New Member
Joined
Jan 27, 2010
Messages
27
I'm stuck. I need a formula to extract data from between two characters.

For Example, In A1 I have this: COMP_PROG_v1_ABCD_01

I want to extract the value between the 3rd and 4th "_"'s. The number of "_"'s will be consistent but not the # of characters between them. My brain is tired of thinking. Thanks.
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
In A1: QWER A31(10)C1A2.1/4.F.a.SDA3.Z
In B1 =F_snb(A1,2)

In a macromodule

Code:
Function F_snb(c00, y)
  c00 = Replace(c00, " ", "")

  For j = 1 To Len(c00)
     If Val(Mid(c00, j)) > 0 Then
        c01 = c01 & " " & Format(Val(Mid(c00, j)))
        j = j + Len(Format(Val(Mid(c00, j)))) - 1
     Else
        c01 = c01 & Mid(c00, j, 1)
     End If
  Next
  
  F_snb = Val(Split(c01)(y))
End Function
 
Upvote 0
In A1: QWER A31(10)C1A2.1/4.F.a.SDA3.Z
In B1 =F_snb(A1,2)

In a macromodule

Code:
Function F_snb(c00, y)
  c00 = Replace(c00, " ", "")

  For j = 1 To Len(c00)
     If Val(Mid(c00, j)) > 0 Then
        c01 = c01 & " " & Format(Val(Mid(c00, j)))
        j = j + Len(Format(Val(Mid(c00, j)))) - 1
     Else
        c01 = c01 & Mid(c00, j, 1)
     End If
  Next
  
  F_snb = Val(Split(c01)(y))
End Function
Your code does not return what the OP asked for in Message #55 for the first "number"... he wanted "31(10)" returned whereas your function returns just "31". As for other symbols, we are left up in the air... in my lastest code (Message #59), I assumed forward slashes would remain with the numbers whereas you did not, but we will need the OP to come back and clarify how the numbers and symbols should actually be grouped (if at all).
 
Upvote 0
The character before the second set of number could have parentheses.
You mention that the character before could have parentheses but this time you haven't mentioned whether the second set set of numbers could also have parentheses like the first set. Like Rick, I'm assuming there could be parentheses as well so AWOIJ AG412W(P)2(3)S3.9/2asind would yield "2(3)" for the second set.

Code:
Function GetNum(S As String, Optional WhichNum As Long = 1) As String
  With CreateObject("VBScript.RegExp")
    .Global = True
    .Pattern = "((\(\d+\))|\d+)+"
    GetNum = .Execute(S)(WhichNum - 1)
  End With
End Function

Formula in B2 is copied across and down.

Excel Workbook
ABC
1StringSet 1Set 2
2QWER A31(10)C1A2.1/4.F.a.SDA3.Z31(10)1
3WIENS EA532Q5B3/2j9fak5325
4AWOIJ AG412W(P)2S3.9/2asind4122
5AWOIJ AG412W(P)2(3)S3.9/2asind4122(3)
Sheet1




Rick, I'd recommend referencing the column(s) the formulas are actually in when constructing the worksheet formula.
As it stands, subsequently inserting a new column A or B would mean your function returns incorrect results.
 
Upvote 0
I have a similar issue, I have been scratching my head with this for days.

I am needing to only extract the series that is in bold and underlined. The problem with those series is, there are hyphens, spaces, texts, numbers. Any way this is possible?

Sender: </SPAN>SBS R-10034793-001000 Inconsistency between a document field and the prof. seg</SPAN>
Sender: SBS P-ALTBBJ1 center N10/6024 is blocked against indirect post</SPAN>
Sender: SBS C-00128 INS Too Cap - NIKON Laser N00/6199 is blocked</SPAN>
Sender: SBS C-40002-1 -5 -1 AUTOBODY POLISHING Settlement Receiver Type SBS Not Allowed


I thank you in advanced. </SPAN>

<TBODY>
</TBODY><COLGROUP><COL></COLGROUP>
 
Upvote 0
I have a similar issue, I have been scratching my head with this for days.

I am needing to only extract the series that is in bold and underlined. The problem with those series is, there are hyphens, spaces, texts, numbers. Any way this is possible?

Sender: SBS R-10034793-001000 Inconsistency between a document field and the prof. seg
Sender: SBS P-ALTBBJ1 center N10/6024 is blocked against indirect post
Sender: SBS C-00128 INS Too Cap - NIKON Laser N00/6199 is blocked
Sender: SBS C-40002-1 -5 -1 AUTOBODY POLISHING Settlement Receiver Type SBS Not Allowed


I thank you in advanced.

<tbody>
</tbody>
** DUPLICATE QUESTION **

You asked this same question here...

http://www.mrexcel.com/forum/excel-...th-numeric-extraction-string.html#post4074554

and I responded with a couple of question that you need to answer. You should not post the same question in multiple threads (theoretically, you should not jump on the end of someone else's thread, but your question seemed related enough to the above quoted link for me not to admonish you for doing that... there). The problem with multiple postings is the volunteers here will not know about each thread the question is in and quite possibly waste their time providing you with the same answer that you got at the other thread... that is not fair to the volunteers you are asking to help you... so please don't multi-post like that in the future.
 
Upvote 0
i was wondering if anyone can please help with the formula that can extract ALL PERCENTAGES DATA E.G 70% viscose 25% nylon 5% elastane.

Data is divided like this
80% cotton 20% nylon.
Petite sizes 6 - 18 length 34"/86.5 cm<br/>Regular sizes 6R - 22R length 36.5"/93 cm<br/>Tall sizes 8T - 18T length 38"/96.5 cm<br/><br/>70% viscose 25% nylon 5% elastane.


result should be like
80% cotton 20% nylon.
70% viscose 25% nylon 5% elastane.
 
Upvote 0
I have a similar issue I can't resolve.

A1 : SS1 \nLocation: Village 1 \nTutor: AGP
B1 : SS2 \nLocation: Village 2 \nTutor: GS4

I'm trying to extract the Location Name - Village 1 Village 2 etc. and have managed to substitute fixed characters with =SUBSTITUTE(SUBSTITUTE(A1," \nLocation: ","|")," \nTutor: ","|") :confused: and tried to rework what Ron has done, but get lost in the formula!

Thanks in advance for your assistance.
 
Upvote 0
Just to add to this - the SS1 and AGP fields are variable in length, the only constants are \nLocation: and \nTutor:

Thanks
 
Upvote 0
This one is really powerful. I just changed the number from "3" to "2", the extracted characters changed accordingly. So this formula literally can be used to extract the characters between any two "_"s. Thanks.
 
Upvote 0

Forum statistics

Threads
1,216,215
Messages
6,129,560
Members
449,516
Latest member
lukaderanged

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