Results 1 to 5 of 5

Thread: Convert inches to feet

  1. #1

    Thread Starter
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901

    Convert inches to feet

    This might help someone.
    Please feel free to optimize

    VB Code:
    1. Option Explicit
    2.  
    3. Public Function convertInches(inchesToConvert) 'inchesToConvert
    4. Dim Holder 'inchesToConvert
    5. Dim inchesLeft
    6. Dim inchesRight
    7. Dim inches
    8. Holder = inchesToConvert
    9.  
    10. Dim Feetdec As Single
    11.     Feetdec = CStr(Holder / 12) ' inches converted to Feet
    12.                                                  
    13.                                                  
    14. Dim feetLeft
    15. On Error Resume Next
    16.     feetLeft = Left(Feetdec, InStrRev(Feetdec, ".") - 1) ' calculating feet no decimal place
    17.     If Err.Number <> 0 Then
    18.     Err.Clear
    19.     feetLeft = Feetdec
    20.     End If
    21.                                              
    22.     inches = Holder - (feetLeft * 12)
    23.                                                
    24.     inchesRight = CSng(Right(inches, Len(inches) - InStrRev(inches, ".") + 1)) ' problem if inches are right on.
    25.     If InStrRev(inchesRight, ".") = 0 Then
    26.         inchesRight = 0
    27.     End If
    28.    
    29. On Error Resume Next
    30.  
    31.     inchesLeft = CSng(Left(inches, InStrRev(inches, ".") - 1))
    32.     If Err.Number <> 0 Then
    33.     inchesLeft = inches
    34.     End If
    35.    
    36.  
    37. Dim Formatedfraction
    38. Select Case inchesRight
    39.  
    40.     Case Is = 0
    41.         Formatedfraction = vbNullString
    42.     Case Is <= 0.125
    43.         Formatedfraction = " 1/8"
    44.     Case Is <= 0.1875
    45.         Formatedfraction = " 3/16"
    46.     Case Is <= 0.25
    47.         Formatedfraction = " 1/4"
    48.     Case Is <= 0.3125
    49.         Formatedfraction = " 5/16"
    50.     Case Is <= 0.375
    51.         Formatedfraction = " 3/8"
    52.     Case Is <= 0.4375
    53.         Formatedfraction = " 7/16"
    54.     Case Is <= 0.5
    55.         Formatedfraction = " 1/2"
    56.     Case Is <= 0.5625
    57.         Formatedfraction = " 9/16"
    58.     Case Is <= 0.625
    59.         Formatedfraction = " 5/8"
    60.     Case Is <= 0.6875
    61.         Formatedfraction = " 11/16"
    62.     Case Is <= 0.75
    63.         Formatedfraction = " 3/4"
    64.     Case Is <= 0.8125
    65.         Formatedfraction = " 13/16"
    66.     Case Is <= 0.875
    67.         Formatedfraction = " 7/8"
    68.     Case Is <= 1
    69.         Formatedfraction = vbNullString
    70.         inchesLeft = inchesLeft + 1
    71. End Select
    72.    
    73. convertInches = feetLeft & "'" & "-" & "" & inchesLeft & "" & Formatedfraction & Chr(34)
    74.  
    75. End Function


    Seahag

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    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.

  3. #3

    Thread Starter
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901
    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

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    Curiosity question: Why are all your variables Variants except for FeetDec? Why not Integers instead?

  5. #5

    Thread Starter
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901
    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
  •  



Click Here to Expand Forum to Full Width