String Fractions Convert to Numbers VBA

J

jlclyde

I have items like 4 7/8 X 7 in a cell. I am trying to put together a
function to multiply the two numbers together. I can do this with
normal functions in Excl but I would like a function to do this job.
So I would like this to return 34.125. Here is the code I have.

Any help is appreciated,
Jay

Private Function FCArea() As String
Dim Fnd As String
Dim L As String, R As String, x As Integer

Fnd = Range("B:B").Find(What:="Final Size:").Offset(2, 0).Value

x = InStr(1, Fnd, "X", 1)

L = Left(Fnd, x - 1)
R = Right(Fnd, Len(Fnd) - x)
FCArea = L * R


End Function
 
J

jlclyde

I have items like 4 7/8 X 7 in a cell.  I am trying to put together a
function to multiply the two numbers together.  I can do this with
normal functions in Excl but I would like a function to do this job.
So I would like this to return 34.125.   Here is the code I have.

Any help is appreciated,
Jay

Private Function FCArea() As String
    Dim Fnd As String
    Dim L As String, R As String, x As Integer

    Fnd = Range("B:B").Find(What:="Final Size:").Offset(2, 0).Value

    x = InStr(1, Fnd, "X", 1)

    L = Left(Fnd, x - 1)
    R = Right(Fnd, Len(Fnd) - x)
    FCArea = L * R

End Function

I found a way to convert each fraction to a decimal and then multiply
them. Here is that code.
Jay

Function Frac2Num(ByVal X As String) As Double
Dim P As Integer, N As Double, Num As Double, Den As Double
X = Trim$(X)
P = InStr(X, "/")
If P = 0 Then
N = Val(X)
Else
Den = Val(Mid$(X, P + 1))
If Den = 0 Then Error 11 ' Divide by zero
X = Trim$(Left$(X, P - 1))
P = InStr(X, " ")
If P = 0 Then
Num = Val(X)
Else
Num = Val(Mid$(X, P + 1))
N = Val(Left$(X, P - 1))
End If
End If
If Den <> 0 Then
N = N + Num / Den
End If
Frac2Num = N
End Function
 
G

Gary''s Student

An interesting question.

Function FCArea(r As Range) As Variant
' gsnuxx
s = Split(r.Value, " X ")
FCArea = Evaluate(s(0)) * Evaluate(s(1))
End Function

so if D1 contained the string:
7 7/8 X 7
=FCArea(D1) would display 55.125
 
M

Mike H

Hi,

maybe

Function EvalFrac(r As Range)
EvalFrac = Application.Evaluate(Replace(LCase$(r.Value), "x", "*"))
End Function

call with

=EvalFrac(a1)

Mike
 

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