Custom UDF to Extract Numbers

sbeatton

Active Member
Joined
May 19, 2004
Messages
411
I am trying to write a UDF to extract sections of formulae from the below. I found the custom UDF below but not having much fun trying to understannd it. Am I going about this the right way / can someone please assist (even if setting me on the right track).

Excel Workbook
CDEF
1FormulaPart 1Part 2Part 3
2$ 1,126.871126.8700
3$ 753.26751.2602
4$ 356.45356.45
Sheet1


This is the UDF I am tring to utilise (copied from PGC01 http://www.mrexcel.com/forum/showthread.php?t=308297&highlight=split+formula+number)
Code:
Function Getnumber(r As Range, lIndex As Long) As Variant
Dim oMatches As Object
 
    If r.Count > 1 Then Exit Function
    
    With CreateObject("vbscript.regexp")
        .Pattern = "\W\d+"
        .Global = True
        
        Set oMatches = .Execute(r.Formula)
    End With
        
    If oMatches.Count >= lIndex Then
        Getnumber = Mid(oMatches(lIndex - 1), 2)
    Else
        Getnumber = ""
    End If
End Function
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
This might be clearer.
I have a formula with three sets of brackets. I need to extract each into an individual cell.
i.e A1 = (1)+(2)+(3)
Result will be
A2 = 1
A3 = 2
A4 = 3
Thanks
 
Upvote 0
Hi,

Is it you wanted?
Code:
Function GetNumber(r As Range, Optional lIndex = 1) As String
  Dim f As String, arr
  f = r.Formula
  If Mid$(f, 1, 1) = "=" Then f = Mid$(f, 2)
  f = Replace(f, "(", "~")
  f = Replace(f, ")", "~")
  arr = Split(f, "~")
  On Error Resume Next
  GetNumber = arr(lIndex * 2 - 1)
End Function

Sub test()
  [A1].Formula = "=(1048.25+78.62)+(0)+(0)"
  Debug.Print GetNumber([A1], 1)
  Debug.Print GetNumber([A1], 2)
  Debug.Print GetNumber([A1], 3)
  [A2].Value = "'" & GetNumber([A1], 1)
  [A3].Value = "'" & GetNumber([A1], 2)
  [A4].Value = "'" & GetNumber([A1], 3)
End Sub

Regards,
Vladimir
 
Last edited:
Upvote 0
Here's a possible alternative:

Code:
Function GetNum(s As String, Optional indx As Integer = 1)
With CreateObject("vbscript.regexp")
    .Global = True
    .Pattern = "\([\-\d\.]+"
    If .test(s) Then GetNum = Replace(.Execute(s)(indx - 1), "(", "")
End With
End Function

Use in a cell like:

=GetNum($A1,ROWS($A$1:A$1))

copied across.
 
Upvote 0
For Richard:
Hi! It is my opinion that your variant with scripting object library would be more required as it was used in request. But please take into account that it does not work for requested parsing of FORMULA (not value) in cell - refer to C2 formula in sbeatton example above.
Also GetNum() returns "1048.25" instead of required "1048.25+78.62" for mentioned formula in C2, some improving of your script is required, I think.

For sbeatton:
1. You can use cell formula for calling my version of UDF like this:
=GetNumber(C2, 1) or =GetNumber(C2, 2) and so on.
2. GetNumber() parses the cell formula as well as the string
3. If the 2nd index parameter exceeds available range, than GetNumber() returns an empty string "", if you need in Error return - just comment this line of code: On Error Resume Next.

Regards,
Vladimir
 
Last edited:
Upvote 0
Hi Vladimir

You are absolutely correct of course - I never read the OP's original question properly, hence my suggestion didn't come close to providing a solution :oops:

Hopefully this one works better:

Code:
Function GetNum(r As Range, Optional indx As Integer = 1)
With CreateObject("vbscript.regexp")
    .Global = True
    .Pattern = "\(.+?\)"
    If .test(r.Formula) Then GetNum = Replace(Replace(.Execute(r.Formula)(indx - 1), "(", ""), ")", "")
End With
End Function
 
Upvote 0

Forum statistics

Threads
1,215,330
Messages
6,124,307
Members
449,151
Latest member
JOOJ

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