I have two long formulas that I am trying to turn into a UDF because I use them frequently, and I am new to VBA.
The first formula converts the tick price (Agency MBS convention) into a decimal number. The convention is shown below.
Formula:
=LEFT(A1,FIND("-",A1)-1)+(LEFT(SUBSTITUTE(RIGHT(A1,LEN(A1)-FIND("-",A1)),"+","4"),2)+IFERROR(MID(SUBSTITUTE(RIGHT(A1,LEN(A1)-FIND("-",A1)),"+","4"),3,1)/8,0))/32
ticktodec.PNG
For 104-084, the "08" to the right of "-" represents 8/32's and the third number represents 4/8 of 1/32 or alternatively 4/256. The "+" means half of 1/32 or 4/256 as well.
I created a UDF for this formula(=Pxd(n)), but it only works for 2 of the 4 different tick formats I need.
Function Pxd(n)
Pxd = Left(n, WorksheetFunction.Find("-", n) - 1) + (Left(WorksheetFunction.Substitute( _
Right(n, Len(n) - WorksheetFunction.Find("-", n)), "+", "4"), 2) _
+ WorksheetFunction.IfError(Mid(WorksheetFunction.Substitute(Right(n, Len(n) - _
WorksheetFunction.Find("-", n)), "+", "4"), 3, 1) / 8, 0)) / 32
End Function
I tried to create a UDF for the second formula, which reverses the formula and converts decimals to ticks but kept running into errors.
Formula:
=INT(A2) & "-"&IF(LEN(ROUNDDOWN((A2-INT(A2))*32,0))=1,"0"&ROUNDDOWN((A2-INT(A2))*32,0),ROUNDDOWN((A2-INT(A2))*32,0))&VLOOKUP((ROUNDDOWN((A2-INT(A2))*256,0)/8)-INT(ROUNDDOWN((A2-INT(A2))*256,0)/8),{0,0;0.125,1;0.25,2;0.375,3;0.5,"+";0.625,5;0.75,6;0.875,7;1,8},2,0)
dectotick.PNG
How do I fix the first UDF and how should I set up the UDF for the second formula? Thank you in advance for any help you can provide.
Bookmarks