VBA to convert fractions

mark9097

New Member
Joined
Jan 25, 2007
Messages
22
Hi,

Can someone help me, what i need is some vba code to convert a price with a fraction in it to a decimal figure.

Eg in cell A1 you have 1.03 1/2
B1 56.05 47/64
ect
i need to convert to 1.035

thanks
Mark
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Hi Mark

There may be easier ways, but the following will work:

Code:
Sub ConvFractions()
Dim r As Range, strText As String
For Each r In Selection
    strText = Replace(Application.WorksheetFunction.Trim(r.Text), " ", "+")
    r.Value = Evaluate(strText)
Next
End Sub

Will convert all cells in the selected range at the point it is run.
 
Upvote 0
Thanks for that,

Just one more thing though, it works well but adds the fraction on to the price instead of being the next decimal placing

eg 1.03 1/5 becomes 1.53 not 1.035

but thing get a bit more complicated when we get to 102.05 47/64, the fraction shows as 0.74 but adding it on to the price will geve me 102.050.74

Hope this makes sense
 
Upvote 0
Sub ConvFractions()
Dim r As Range, strText As String
For Each r In Selection
strText = Replace(Application.WorksheetFunction.Trim(r.Text), " ", "+")
r.Value = Evaluate(strText)
Next
End Sub

If i replace the + with a & then i tags the fraction amount on the end, but then on some prices i get a figure like 103.35.005 which should be 103.35005.

can anyone tell me how to get the second "." out of the number.

Thanks
 
Upvote 0
Hi

Sorry, failed to appreciate exactly what it was you were after - give this one a shot:

Code:
Sub ConvFractions()
Dim r As Range, aVal As Variant, strText As String
For Each r In Selection
    aVal = Split(Application.WorksheetFunction.Trim(r.Text))
    strText = aVal(0) & Replace(Format(Evaluate(aVal(1)), "0.000000"), "0.", "")
    r.Value = Evaluate(strText)
Next
End Sub
 
Upvote 0
Perhaps a UDF?
Code:
Function ConvFrac(rng As Range)
Dim arrVals
    arrVals = Split(rng.Value, " ")
    
    ConvFrac = arrVals(0) & Mid(Evaluate(arrVals(1)), 3)
End Function
 
Upvote 0

Forum statistics

Threads
1,215,248
Messages
6,123,869
Members
449,130
Latest member
lolasmith

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