UDF involving Split function or Formula Text?

VBE313

Well-known Member
Joined
Mar 22, 2019
Messages
686
Office Version
  1. 365
Platform
  1. Windows
I created a UDF that calculates how many minutes it takes to complete a certain task.

(ex: =MinutesToComplete(1,0.18,10,50,20) )

Is there a way to extract each argument/parameter?

(ex: =RightParameter() ) and the answer would be 20
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
I got this function from here and it gave me the all the numbers listed out in commas,
ex:) 1,0.18,10,50,20

How can I get these numbers separated?

https://www.mrexcel.com/forum/excel-questions/327437-custom-udf-extract-numbers.html


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
 
Upvote 0
This UDF might help.
If a cell contains a formula that is only one function, like =MyUDF(arg1, arg2, arg3) this will return the indicated argument in that formula.
Note that commas inside quote marks e.g. =LEN("abc,xyz,def") are not counted as argument seperators

Code:
Function FunctionArguments(formulaCell As Range, ArgumentIndex As Long) As String
    Dim strFormula As String
    Dim Pointer As Long, i As Long, flag As Boolean
    strFormula = formulaCell.Cells(1, 1).Formula
    If strFormula Like "*)" Then strFormula = Left(strFormula, Len(strFormula) - 1) & ","
    For Pointer = 1 To Len(strFormula)
        If Mid(strFormula, Pointer, 1) = "(" Then Exit For
    Next Pointer
    
    If Len(strFormula) < Pointer Then
        Rem no function e.g. =5
        FunctionArguments = "-"
    Else
        Do
            Pointer = Pointer + 1
            If (Not flag) And (Mid(strFormula, Pointer, 1) = ",") Then
                Rem found one argument
                ArgumentIndex = ArgumentIndex - 1
                If ArgumentIndex = 0 Then
                    Exit Function
                Else
                    FunctionArguments = vbNullString
                End If
            Else
                FunctionArguments = FunctionArguments & Mid(strFormula, Pointer, 1)
                If Mid(strFormula, Pointer, 1) = Chr(34) Then flag = Not flag
            End If
        Loop Until Len(strFormula) < Pointer
    End If
End Function
 
Upvote 0
mikerickson,

Thank you very much! It is doing what I wanted and was even easier than what I had.

THANK YOU!
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
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