Convert cell formula to VBA

Oxalate

New Member
Joined
Apr 30, 2017
Messages
5
Hello, is there a hero to help me convert a cell formula to VBA code with a custom function?
The cell formula takes 2 selected values and calculates the difference between them, and then shows the result as "xx % difference".
The difference is always calculated in regards to the higher value:

(higher value - lower value)/higher value * 100 %
So the funktion has to check which vaule is higher and then do the calculation.

The problem is that in the cell there is not always just a number (that would be easy), but sometimes the cell is made of text and numbers e.g. "< LOQ (0.05)", so the formula has to "extract" the number and then calculate further. I have made a cell function but its really long and i have to adjust the cell reference multiple times.
Could someone please make a custom named VBA function where I select the cells and excel does the rest, for example =difference(A1,A2).

This is the function (of course any function that works will be ok too):

IF(OR(A1="< limit ",A2="< limit "),"Value close to or at limit",IFERROR(IF(A1>=A2,ROUND((((A1-A2)/A1)*100),0)&" % Difference",ROUND((((A2-A1)/A2)*100),0)&" % Difference"),IF(--MID(A1,MIN(IFERROR(FIND({0,1,2,3,4,5,6,7,8,9,".",","},A1),"")),COUNT(--MID(SUBSTITUTE(SUBSTITUTE(A1,".",0),",",0),ROW($1:$100),1)))>=--MID(A2,MIN(IFERROR(FIND({0,1,2,3,4,5,6,7,8,9,".",","},A2),"")),COUNT(--MID(SUBSTITUTE(SUBSTITUTE(A2,".",0),",",0),ROW($1:$100),1))),ROUND((((--MID(A1,MIN(IFERROR(FIND({0,1,2,3,4,5,6,7,8,9,".",","},A1),"")),COUNT(--MID(SUBSTITUTE(SUBSTITUTE(A1,".",0),",",0),ROW($1:$100),1)))---MID(A2,MIN(IFERROR(FIND({0,1,2,3,4,5,6,7,8,9,".",","},A2),"")),COUNT(--MID(SUBSTITUTE(SUBSTITUTE(A2,".",0),",",0),ROW($1:$100),1))))/--MID(A1,MIN(IFERROR(FIND({0,1,2,3,4,5,6,7,8,9,".",","},A1),"")),COUNT(--MID(SUBSTITUTE(SUBSTITUTE(A1,".",0),",",0),ROW($1:$100),1))))*100),0)&" % Difference",ROUND((((--MID(A2,MIN(IFERROR(FIND({0,1,2,3,4,5,6,7,8,9,".",","},A2),"")),COUNT(--MID(SUBSTITUTE(SUBSTITUTE(A2,".",0),",",0),ROW($1:$100),1)))---MID(A1,MIN(IFERROR(FIND({0,1,2,3,4,5,6,7,8,9,".",","},A1),"")),COUNT(--MID(SUBSTITUTE(SUBSTITUTE(A1,".",0),",",0),ROW($1:$100),1))))/--MID(A2,MIN(IFERROR(FIND({0,1,2,3,4,5,6,7,8,9,".",","},A2),"")),COUNT(--MID(SUBSTITUTE(SUBSTITUTE(A2,".",0),",",0),ROW($1:$100),1))))*100),0)&" % Difference")))

Thank you in advance.
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.

Book1
ABC
1< LOQ(0.05)0.0616.67 % difference
27528.57 % difference
30.5 0.60.7#ERROR
4< limit3Value close to or at limit
5100991.00 % difference
Sheet1
Cell Formulas
RangeFormula
C1=GetDifference(A1,B1)


Code:
Private regex As Object
Public Function GetDifference(range1 As Range, range2 As Range) As String

Dim matches1 As Object
Dim matches2 As Object
Dim value1 As Double
Dim value2 As Double
Dim diffValue As Double

' Set up the object
If regex Is Nothing Then Set regex = CreateObject("VBScript.RegExp")

' Check for special condition
If Left$(range1.Value, 7) = "< limit" Or Left$(range2.Value, 7) = "< limit" Then
    GetDifference = "Value close to or at limit"
    Exit Function
End If

' Find the numbers in the ranges
With regex
    .Pattern = "([\d\.,]+)"
    .Global = True
    Set matches1 = .Execute(range1.Value)
    Set matches2 = .Execute(range2.Value)
End With

' Check there's only one number
If matches1.Count <> 1 Or matches2.Count <> 1 Then
    GetDifference = "#ERROR"
    Exit Function
End If

' Get the values
value1 = CDbl(matches1(0).Value)
value2 = CDbl(matches2(0).Value)

' Calculate the % difference
If value1 > value2 Then
    diffValue = (value1 - value2) / value1 * 100
Else
    diffValue = (value2 - value1) / value2 * 100
End If

' Format the result
GetDifference = Format$(diffValue, "0.00") & " % difference"

End Function

WBD
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,021
Latest member
Mohamed Magdi Tawfiq Emam

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