Extracting text from a string with variable length

TobiasL

New Member
Joined
Jun 9, 2008
Messages
38
Hey I got a long String like this "[...] increase of x.xx% [...]".

I am trying to extract only the percentage number which can be of variable length, so maybe 900.99% or 9.99%.

I tried this formula:
Code:
=MID(G14,SEARCH("%",G14)-5,5)
but this one doesnt bring the right results as the percentage figure is often not exactly 5 characters long.

Thanks for any help!

Tobi
 
=NumberBeforeChar(A1,"-")
Code:
Function NumberBeforeChar(ByVal txt As String, ByVal myChr As String) As Double
Select Case myChr
    Case "[", "]", "(", ")", "{", "}", "+", "*", "?", "$", "^", "\", ".", "|"
    myChr = "\" & myChr
End Select
With CreateObject("VBScript.RegExp")
    .Pattern = "(\d+(\.\d+)?)" & myChr
    NumberBeforeChar = .execute(txt)(0).SubMastches(0)
End With
End Function

hey thanks again for the script. The problem is that when I am pasting that function in 500 cells and more, that Excel gets really slow and recalculates these cells in short timestamps.

Any help for that?

thanks
Tobi
 
Upvote 0

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Early binding will considerably speed up performance of the code proposed by Jindon.

Alternatively this one can be used as well:
Rich (BB code):
<font face=Courier New>
' Returns number value before % char in Ref string
' =PC(A1) or =PC(A1,1) returns the 1st case (ocurrence) of percent value in A1
' =PC(A1,2) returns the 2nd case of percent value in A1, and so on
Function PC(Ref As String, Optional Num As Integer = 1)
  Dim s$, arr
  PC = ""
  arr = Split(Ref, "%")
  If Num > UBound(arr) + 1 Or Num < 1 Then Exit Function
  s = Trim$(arr(Num - 1))
  s = Mid$(s, InStrRev(s, " ") + 1)
  If IsNumeric(Mid(s, 1, 1)) Then PC = Val(s)
End Function</FONT>
Regards,
Vladimir
 
Last edited:
Upvote 0
To be more correct please replace this line of code:

If Num > UBound(arr) + 1 Or Num < 1 Then Exit Function

to:
If Num > UBound(arr) Or Num < 1 Then Exit Function
 
Upvote 0
Early binding will considerably speed up performance of the code proposed by Jindon.

Alternatively this one can be used as well:
Rich (BB code):

' Returns number value before % char in Ref string
' =PC(A1) or =PC(A1,1) returns the 1st case (ocurrence) of percent value in A1
' =PC(A1,2) returns the 2nd case of percent value in A1, and so on
Function PC(Ref As String, Optional Num As Integer = 1)
  Dim s$, arr
  PC = ""
  arr = Split(Ref, "%")
  If Num > UBound(arr) + 1 Or Num < 1 Then Exit Function
  s = Trim$(arr(Num - 1))
  s = Mid$(s, InStrRev(s, " ") + 1)
  If IsNumeric(Mid(s, 1, 1)) Then PC = Val(s)
End Function
Regards,
Vladimir

Hey vladimir, thanks for helping. Could you explain how I can implement early binding in Jindon's code?

Thanks
 
Upvote 0
Hi Tobi,

To set early binding do as follows:
1. In Excel go to VBE by pressing of Alt-F11
2. Choose menu: Tools-References
3. Find in a references list the item named as Microsoft VBScript Regular Expression 5.5 and set On its checkbox.
4. Replace this line of Jindon's code: With CreateObject("VBScript.RegExp")
by that one: With New RegExp

Regards,
Vladimir
 
Last edited:
Upvote 0
Early binding will considerably speed up performance of the code proposed by Jindon.

Alternatively this one can be used as well:
Rich (BB code):

' Returns number value before % char in Ref string
' =PC(A1) or =PC(A1,1) returns the 1st case (ocurrence) of percent value in A1
' =PC(A1,2) returns the 2nd case of percent value in A1, and so on
Function PC(Ref As String, Optional Num As Integer = 1)
  Dim s$, arr
  PC = ""
  arr = Split(Ref, "%")
  If Num > UBound(arr) + 1 Or Num < 1 Then Exit Function
  s = Trim$(arr(Num - 1))
  s = Mid$(s, InStrRev(s, " ") + 1)
  If IsNumeric(Mid(s, 1, 1)) Then PC = Val(s)
End Function
Regards,
Vladimir

Hey vladimir could you show me how I can modify your code so that I can extract different patterns. For example I have a string like this "At the time this buy order was entered, it was x7,115 shares from the priority bid"
I want to extract all figures which have an "x" in the beginning (7,115)

Thanks again!
 
Upvote 0
Hey vladimir could you show me how I can modify your code so that I can extract different patterns. For example I have a string like this "At the time this buy order was entered, it was x7,115 shares from the priority bid"
I want to extract all figures which have an "x" in the beginning (7,115)

Thanks again!

TobiasL

Open new thread for this and it can be done by Formula without vba.
 
Upvote 0
This might do it for you
=MID(A1,MIN(FIND({1,2,3,4,5,6,7,8,9,0},A1&"1234567890")), FIND("%",A1&"%")-MIN(FIND({1,2,3,4,5,6,7,8,9,0},A1&"1234567890")))
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

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