Function to convert string
On Sun, 18 Jul 2004 16:13:13 -0700, keepITcool wrote:
Ron...
Try "100x254x101" on your function below it reutrns:
3 1/1" iso 4"
been doing some testing.. must say your function is very fast..
Yes there's a rounding issue there. But this should work:
==================================
Option Explicit
Function ConMet(Val As Range) As String
Dim Dimensions As Variant
Dim i As Integer, j As Integer
Dim FractNumerator As Integer
Dim FractDenominator As Integer
Dim Fraction As String
Dim Sep As String
Const mmPerInch As Double = 25.4
For i = 1 To Len(Val.Text)
Sep = Mid(Val.Text, i, 1)
If Not IsNumeric(Sep) Then Exit For
Next i
If IsNumeric(Sep) Then Sep = ""
Dimensions = Split(Val.Text, Sep, -1)
For i = 0 To UBound(Dimensions)
Dimensions(i) = Round(Dimensions(i) / mmPerInch * 16, 0) / 16
ConMet = ConMet & Int(Dimensions(i))
FractNumerator = 16 * (Dimensions(i) - Int(Dimensions(i)))
FractDenominator = 16
For j = 0 To 3
If FractNumerator Mod 2 = 1 Then Exit For
FractNumerator = FractNumerator / 2
FractDenominator = FractDenominator / 2
Next j
If FractNumerator = 0 Then
Fraction = """"
Else
Fraction = " " & FractNumerator & "/" & FractDenominator & """"
End If
ConMet = ConMet & Fraction & " " & "X" & " "
Next i
ConMet = Left(ConMet, Len(ConMet) - 3)
End Function
===============================
i reviemwed my own code:
found that TEXT is a member of application.worksheetfunction,
so I can do without the evaluate..
included the " and trimmed the result..
made decimal entry possible... (IF locale has . as decimal)
this is 14% slower then your code but as fast and concise
as I can make it:
Function ConvMM2(Dimensions As String)
Dim i%, v
Const mm2in# = 25.4
On Error GoTo oops:
If IsNumeric(Dimensions) Then
v = Array(Dimensions)
Else
Do
i = i + 1
Loop While IsNumeric(Left(Dimensions, i))
v = Split(Dimensions, Mid(Dimensions, i, 1))
End If
With Application
For i = LBound(v) To UBound(v)
v(i) = .Trim(.Text(v(i) / 25.4, "# ??/16")) & """"
Next
End With
ConvMM2 = Join(v, " X ")
Exit Function
oops:
ConvMM2 = CVErr(xlErrNA)
End Function
That seems to give the correct answer now. Although it does not simplify the
fractions.
--ron
|