convert fractions to decimals

S

Shannon

Does anyone kow if you can convert the following fraction to a decimal
automatically.

7/8 x 2 1/4?

Thanks in advance, I will score big brownie points with the boss if we can
figure this one out :)
 
P

PCLIVE

Where are these fractions? Are they in separate cells? Or is it in one
cell just like that (7/8 x 2 1/4)? If that is the case, then technically,
it is not a fraction, but text.

Separate cells and actual fractions?...change the cell format to Number.


Regards,
Paul
 
R

Rick Rothstein \(MVP - VB\)

Can you use a macro? If so, you can have your macro call this subroutine...

Sub Solve(R As Range)
Dim C As Range
For Each C In R
C.Value = Application.Evaluate(Replace(LCase$(C.Value), "x", "*"))
Next
End Sub

Simply have your macro pass in the range of cells you want to convert from
fractional calculations to numerical values. Something like this...

Sub TestMacro()
Solve Range("D10:D12")
End Sub

Note, as long as you use either "x", "X" or "*" for multiplication, "/" for
division, "+" for addition and "-" for subtraction, coupled with lots of
other functions (such as Sin, Sqrt, etc.) and parentheses, you can do quite
complicated calculations with it.

Rick
 
S

Shannon

Thank you very much, I have teach myself macros first, LOL. Hopefully this
weekend.
Thanks again,
Shannon
 
R

Rick Rothstein \(MVP - VB\)

Let's see if we can get you started at least. Start Excel and go to the
worksheet that has your "fractions" on it. Now, right-click the worksheet's
tab at the bottom of the page and select View Code from the popup menu that
appears. This will take you to the VB editor, and specifically, to the code
window for the worksheet you were on when you right-clicked the tab.
Copy/Paste the following code into that code window...

' ***** Start of Code *****
Sub Solve(R As Range)
Dim C As Range
On Error Resume Next
For Each C In R
C.Value = Application.Evaluate(Replace(LCase$(C.Value), "x", "*"))
If IsError(C.Value) Then C.Clear
Next
End Sub

Sub SolveCellExpressions()
Solve Range("A1:A10")
End Sub
' ***** End of Code *****

Okay, now go back to the worksheet (click File/Close and Return to Microsoft
Excel on the VB editor's menu bar). Type some of your "fractions" and other
expressions (remember, they can be more complicated than simple fractions if
desired) that you want converted into any of the cell in the range A1:A10
(notice, that is the range specified in the subroutine named
SolveCellExpressions). Here are some to get you started...

A1: 7/8 x 2 1/4
A2: Sqrt(1/4)
A3: Sin(0.523598775598299) + 1 1/2

{Answers.... A1 = 1.96875, A2 = 0.5, A3 = 2}

Once you have typed as many expressions as you want in the first 10 cells of
Column A, press Alt+F8 and select SolveCellExpressions from the list that
appears. Next click the Run button and watch what happens to the entries in
the range A1:A10. Hopefully, if all went well, those expressions you typed
in should have been converted to their evaluated values.

Rick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top