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
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Hi there,

If the percentage always follows the text "increase of ", the following should do the trick:

=MID(G14,SEARCH("increase of ",G14)+12,LEN(G14)-SEARCH("increase of ",G14)+12)

HTH

Robert
 
Upvote 0
UDF
1) Hit Alt + F11
2) go to [Insert] - [Module] then paste the code
3) hit Alt + F11 again
Use in cell like
=myPercent(A1)
Code:
Function myPercent(ByVal txt As String) As String
With CreateObject("VBScript.RegExp")
    .Pattern = "\d+(\.\d+)?%"
    myPercent = .execute(txt)(0)
End With
End Function
 
Upvote 0
UDF
1) Hit Alt + F11
2) go to [Insert] - [Module] then paste the code
3) hit Alt + F11 again
Use in cell like
=myPercent(A1)
Code:
Function myPercent(ByVal txt As String) As String
With CreateObject("VBScript.RegExp")
    .Pattern = "\d+(\.\d+)?%"
    myPercent = .execute(txt)(0)
End With
End Function

That works great thanks! Only one thing. Is it possible to get rid of the percentage and convert this string into a number? thanks!
 
Upvote 0
try
Code:
Function myPercent(ByVal txt As String) As Double
With CreateObject("VBScript.RegExp")
    .Pattern = "(\d+(\.\d+)?)%"
    myPercent = .execute(txt)(0).SubMastches(0)
End With
End Function
 
Upvote 0
UDF
1) Hit Alt + F11
2) go to [Insert] - [Module] then paste the code
3) hit Alt + F11 again
Use in cell like
=myPercent(A1)
Code:
Function myPercent(ByVal txt As String) As String
With CreateObject("VBScript.RegExp")
    .Pattern = "\d+(\.\d+)?%"
    myPercent = .execute(txt)(0)
End With
End Function

wondering... can it be done by formula? espically if the number could be ended with any characters including space?
 
Upvote 0
try
Code:
Function myPercent(ByVal txt As String) As Double
With CreateObject("VBScript.RegExp")
    .Pattern = "(\d+(\.\d+)?)%"
    myPercent = .execute(txt)(0).SubMastches(0)
End With
End Function

awesome thanks so much! is it also possible to catch several percentage values with this code?
 
Upvote 0
How do you want the result ?
This will put all numbers in one cell
=myPercent(A1,",")
Code:
Function myPercent(ByVal txt As String, ByVal myJoin As String) As String
Dim m As Object
With CreateObject("VBScript.RegExp")
    .Pattern = "(\d+(\.\d+)?)%"
    .Global = True
    For Each m In .execute(txt)
        myPercent = myPercent & IIf(myPercent = "","", myJoin) & _
            m.submatches(0)
    Next
End With
End Function
This will cell by cell in horizontally
=myPercent($A1,Column(A1))
then copy to the right

if you want them in vertically
=myPercent(A$1,Row(A1))
then copy down
Code:
Function myPercent(ByVal txt As String, ByVal ref As Long)
With CreateObject("VBScript.RegExp")
    .Pattern = "(\d+(\.\d+)?)%"
    myPercent = .execute(txt)(ref - 1).SubMastches(0)
End With
End Function
 
Upvote 0
How do you want the result ?
This will put all numbers in one cell
=myPercent(A1,",")
Code:
Function myPercent(ByVal txt As String, ByVal myJoin As String) As String
Dim m As Object
With CreateObject("VBScript.RegExp")
    .Pattern = "(\d+(\.\d+)?)%"
    .Global = True
    For Each m In .execute(txt)
        myPercent = myPercent & IIf(myPercent = "","", myJoin) & _
            m.submatches(0)
    Next
End With
End Function
This will cell by cell in horizontally
=myPercent($A1,Column(A1))
then copy to the right

if you want them in vertically
=myPercent(A$1,Row(A1))
then copy down
Code:
Function myPercent(ByVal txt As String, ByVal ref As Long)
With CreateObject("VBScript.RegExp")
    .Pattern = "(\d+(\.\d+)?)%"
    myPercent = .execute(txt)(ref - 1).SubMastches(0)
End With
End Function

Awesome again! domo arregato!
 
Upvote 0
wondering... can it be done by formula? espically if the number could be ended with any characters including space?
=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
 
Upvote 0

Forum statistics

Threads
1,215,126
Messages
6,123,198
Members
449,090
Latest member
bes000

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