|
-
Jul 15th, 2002, 01:52 PM
#1
Thread Starter
Fanatic Member
Convert inches to feet
This might help someone.
Please feel free to optimize
VB Code:
Option Explicit
Public Function convertInches(inchesToConvert) 'inchesToConvert
Dim Holder 'inchesToConvert
Dim inchesLeft
Dim inchesRight
Dim inches
Holder = inchesToConvert
Dim Feetdec As Single
Feetdec = CStr(Holder / 12) ' inches converted to Feet
Dim feetLeft
On Error Resume Next
feetLeft = Left(Feetdec, InStrRev(Feetdec, ".") - 1) ' calculating feet no decimal place
If Err.Number <> 0 Then
Err.Clear
feetLeft = Feetdec
End If
inches = Holder - (feetLeft * 12)
inchesRight = CSng(Right(inches, Len(inches) - InStrRev(inches, ".") + 1)) ' problem if inches are right on.
If InStrRev(inchesRight, ".") = 0 Then
inchesRight = 0
End If
On Error Resume Next
inchesLeft = CSng(Left(inches, InStrRev(inches, ".") - 1))
If Err.Number <> 0 Then
inchesLeft = inches
End If
Dim Formatedfraction
Select Case inchesRight
Case Is = 0
Formatedfraction = vbNullString
Case Is <= 0.125
Formatedfraction = " 1/8"
Case Is <= 0.1875
Formatedfraction = " 3/16"
Case Is <= 0.25
Formatedfraction = " 1/4"
Case Is <= 0.3125
Formatedfraction = " 5/16"
Case Is <= 0.375
Formatedfraction = " 3/8"
Case Is <= 0.4375
Formatedfraction = " 7/16"
Case Is <= 0.5
Formatedfraction = " 1/2"
Case Is <= 0.5625
Formatedfraction = " 9/16"
Case Is <= 0.625
Formatedfraction = " 5/8"
Case Is <= 0.6875
Formatedfraction = " 11/16"
Case Is <= 0.75
Formatedfraction = " 3/4"
Case Is <= 0.8125
Formatedfraction = " 13/16"
Case Is <= 0.875
Formatedfraction = " 7/8"
Case Is <= 1
Formatedfraction = vbNullString
inchesLeft = inchesLeft + 1
End Select
convertInches = feetLeft & "'" & "-" & "" & inchesLeft & "" & Formatedfraction & Chr(34)
End Function
Seahag
-
Jul 15th, 2002, 02:03 PM
#2
Holy cow...I have a routine similar to this, but it does not convert down to fractions. I think I'll pop this in my Code Library for possible future use!
Fine job.
-
Jul 15th, 2002, 02:14 PM
#3
Thread Starter
Fanatic Member
Thanks.
Note: The fraction part it only rounds up. Unless you add umpteen case senarios i cant see any way around it.
Any suggestions?
Seahag
-
Jul 16th, 2002, 06:48 AM
#4
Curiosity question: Why are all your variables Variants except for FeetDec? Why not Integers instead?
-
Jul 16th, 2002, 07:30 AM
#5
Thread Starter
Fanatic Member
Well good question.. I was wondering when you were
going to ask.
I did this for excel .. sometime you get conflict when refering to cell values. i find it easier to debug.
Outlook is the same.
Seahag
(or maybe i dont know what i am doing
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|