JenniferMurphy
Well-known Member
- Joined
- Jul 23, 2011
- Messages
- 2,676
- Office Version
- 365
- Platform
- Windows
I was able to find several solutions for converting a value to feet and inches where the inches are expressed as an integer plus a fraction. I couldn't find one that expressed the inches as a floating point number (n.nnn..) with the ability to specify the number of decimal points. So I wrote one myself.
The trick is to convert everything to inches, because that's where the rounding is done. Then the feet can be split off and the remaining inches formatted.
Here's the code and them some examples. I'd appreciate any critiques.
In this table, columns C & D contain the input data. I entered the feet and inches separately so I could compare the results. Column F contains the values in feet (feet + (inches/12). Column G the result of passing that value to the UDF. Columns H & I do the same thing, but with inches. Column J compares the two results to make sure they are identical.
[TABLE="class: grid, width: 550"]
<tbody>[TR]
[TD="align: center"]R/C[/TD]
[TD="align: center"]C[/TD]
[TD="align: center"]D[/TD]
[TD="align: center"]E[/TD]
[TD="align: center"]F[/TD]
[TD="align: center"]G[/TD]
[TD="align: center"]H[/TD]
[TD="align: center"]I[/TD]
[TD="align: center"]J[/TD]
[/TR]
[TR]
[TD="align: center"]3[/TD]
[TD="colspan: 3, align: center"]Input[/TD]
[TD="align: center"]Total[/TD]
[TD="align: center"]Converted[/TD]
[TD="align: center"]Total[/TD]
[TD="align: center"]Converted[/TD]
[TD="align: center"]G = I?[/TD]
[/TR]
[TR]
[TD="align: center"]4[/TD]
[TD="align: center"]Feet[/TD]
[TD="align: center"]Inches[/TD]
[TD="align: center"]DP[/TD]
[TD="align: center"]Feet[/TD]
[TD="align: center"]Result[/TD]
[TD="align: center"]Inches[/TD]
[TD="align: center"]Result
[/TD]
[TD="align: center"]TRUE[/TD]
[/TR]
[TR]
[TD="align: center"]5[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD]0' 0"[/TD]
[TD="align: center"]0[/TD]
[TD]0' 0"
[/TD]
[TD="align: center"]TRUE[/TD]
[/TR]
[TR]
[TD="align: center"]6[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]2[/TD]
[TD="align: center"]0[/TD]
[TD]0' 0.00"[/TD]
[TD="align: center"]0[/TD]
[TD]0' 0.00"
[/TD]
[TD="align: center"]TRUE[/TD]
[/TR]
[TR]
[TD="align: center"]7[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]4[/TD]
[TD="align: center"]0[/TD]
[TD]0' 0.0000"[/TD]
[TD="align: center"]0[/TD]
[TD]0' 0.0000"[/TD]
[TD="align: center"]TRUE[/TD]
[/TR]
[TR]
[TD="align: center"]8[/TD]
[TD="align: center"]2[/TD]
[TD="align: center"]6[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]2.5[/TD]
[TD]2' 6"[/TD]
[TD="align: center"]30[/TD]
[TD]2' 6"[/TD]
[TD="align: center"]TRUE[/TD]
[/TR]
[TR]
[TD="align: center"]9[/TD]
[TD="align: center"]2[/TD]
[TD="align: center"]6[/TD]
[TD="align: center"]3[/TD]
[TD="align: center"]2.5[/TD]
[TD]2' 6.000"[/TD]
[TD="align: center"]30[/TD]
[TD]2' 6.000"[/TD]
[TD="align: center"]TRUE[/TD]
[/TR]
[TR]
[TD="align: center"]10[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]11.55[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]1.9625[/TD]
[TD]2' 0"[/TD]
[TD="align: center"]23.55[/TD]
[TD]2' 0"[/TD]
[TD="align: center"]TRUE[/TD]
[/TR]
[TR]
[TD="align: center"]11[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]11.555[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]1.962917[/TD]
[TD]1' 11.6"[/TD]
[TD="align: center"]23.555[/TD]
[TD]1' 11.6"[/TD]
[TD="align: center"]TRUE[/TD]
[/TR]
[TR]
[TD="align: center"]12[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]11.555[/TD]
[TD="align: center"]3[/TD]
[TD="align: center"]1.962917[/TD]
[TD]1' 11.555"[/TD]
[TD="align: center"]23.555[/TD]
[TD]1' 11.555"[/TD]
[TD="align: center"]TRUE[/TD]
[/TR]
[TR]
[TD="align: center"]13[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]11.555[/TD]
[TD="align: center"]4[/TD]
[TD="align: center"]1.962917[/TD]
[TD]1' 11.5550"[/TD]
[TD="align: center"]23.555[/TD]
[TD]1' 11.5550"[/TD]
[TD="align: center"]TRUE[/TD]
[/TR]
[TR]
[TD="align: center"]14[/TD]
[TD="align: center"]4[/TD]
[TD="align: center"]11.99999[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]4.999999[/TD]
[TD]5' 0"[/TD]
[TD="align: center"]59.99999[/TD]
[TD]5' 0"[/TD]
[TD="align: center"]TRUE[/TD]
[/TR]
[TR]
[TD="align: center"]15[/TD]
[TD="align: center"]4[/TD]
[TD="align: center"]11.99999[/TD]
[TD="align: center"]2[/TD]
[TD="align: center"]4.999999[/TD]
[TD]5' 0.00"[/TD]
[TD="align: center"]59.99999[/TD]
[TD]5' 0.00"[/TD]
[TD="align: center"]TRUE[/TD]
[/TR]
[TR]
[TD="align: center"]16[/TD]
[TD="align: center"]4[/TD]
[TD="align: center"]11.99999[/TD]
[TD="align: center"]4[/TD]
[TD="align: center"]4.999999[/TD]
[TD]5' 0.0000"[/TD]
[TD="align: center"]59.99999[/TD]
[TD]5' 0.0000"[/TD]
[TD="align: center"]TRUE[/TD]
[/TR]
[TR]
[TD="align: center"]17[/TD]
[TD="align: center"]4[/TD]
[TD="align: center"]11.99999[/TD]
[TD="align: center"]5[/TD]
[TD="align: center"]4.999999[/TD]
[TD]4' 11.99999"[/TD]
[TD="align: center"]59.99999[/TD]
[TD]4' 11.99999"[/TD]
[TD="align: center"]TRUE[/TD]
[/TR]
[TR]
[TD="align: center"]18[/TD]
[TD="align: center"]9[/TD]
[TD="align: center"]0.0001[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]9.000008[/TD]
[TD]9' 0"[/TD]
[TD="align: center"]108.0001[/TD]
[TD]9' 0"[/TD]
[TD="align: center"]TRUE[/TD]
[/TR]
[TR]
[TD="align: center"]19[/TD]
[TD="align: center"]9[/TD]
[TD="align: center"]0.0001[/TD]
[TD="align: center"]3[/TD]
[TD="align: center"]9.000008[/TD]
[TD]9' 0.000"[/TD]
[TD="align: center"]108.0001[/TD]
[TD]9' 0.000"[/TD]
[TD="align: center"]TRUE[/TD]
[/TR]
[TR]
[TD="align: center"]20[/TD]
[TD="align: center"]9[/TD]
[TD="align: center"]0.0001[/TD]
[TD="align: center"]4[/TD]
[TD="align: center"]9.000008[/TD]
[TD]9' 0.0001"[/TD]
[TD="align: center"]108.0001[/TD]
[TD]9' 0.0001"[/TD]
[TD="align: center"]TRUE[/TD]
[/TR]
</tbody>[/TABLE]
Thanks
The trick is to convert everything to inches, because that's where the rounding is done. Then the feet can be split off and the remaining inches formatted.
Here's the code and them some examples. I'd appreciate any critiques.
Code:
'**************************************************************************************************
' Convert a Length (number) to Feet & Inches (string)
'
' Syntax: CvtLen2FtIn(pInVal, [pInUnits|"ft"], [pDPOut|2])
'
' pInVal = The number to be converted (real)
' pInUnits = The units for pInVal ("ft" (default) or "in")
' pDPOut = Number of decimal places in the output (default: 2, range: 0-5))
' Result = The converted value as a text field (nn' n.nnn..")
'**************************************************************************************************
Public Function CvtLen2FtIn(pInVal As Double, _
Optional pInUnits As String = "ft", _
Optional pDPOut As Integer = 2) As String
Const MyName As String = "CvtLen2FtIn" 'Function name for error messages
Dim TotInch As Double 'Total length in inches
Dim FeetInt As Double 'Whole number of feet
Dim Inches As Double 'Remaining inches, rounded appropriately
Dim Fmt0s As String 'Format string ("[.]000...")
Dim msg As String 'Text of error messages
'Convert everything to inches. That's where the rounding will be done.
Select Case UCase(pInUnits)
Case "IN"
TotInch = pInVal 'No conversion, it's already in inches
Case "FT"
TotInch = pInVal * 12 'Convert from feet to inches
Case Else
CvtLen2FtIn = CVErr(xlErrValue)
Exit Function
End Select
If pDPOut < 0 Or pDPOut > 7 Then 'Check range of pDPOut (0-5)
CvtLen2FtIn = CVErr(xlErrValue)
Exit Function
End If
Inches = Round(TotInch, pDPOut) 'Round the inches to the specified number of decimal places
FeetInt = Int(Inches / 12) 'Whole number of feet
Inches = Inches - FeetInt * 12 'Remaining inches rounded appropriately
If pDPOut > 0 Then
Fmt0s = "0." & String(pDPOut, "0")
Else
Fmt0s = "0"
End If
CvtLen2FtIn = FeetInt & "' " & Format(Inches, Fmt0s) & """"
End Function
In this table, columns C & D contain the input data. I entered the feet and inches separately so I could compare the results. Column F contains the values in feet (feet + (inches/12). Column G the result of passing that value to the UDF. Columns H & I do the same thing, but with inches. Column J compares the two results to make sure they are identical.
[TABLE="class: grid, width: 550"]
<tbody>[TR]
[TD="align: center"]R/C[/TD]
[TD="align: center"]C[/TD]
[TD="align: center"]D[/TD]
[TD="align: center"]E[/TD]
[TD="align: center"]F[/TD]
[TD="align: center"]G[/TD]
[TD="align: center"]H[/TD]
[TD="align: center"]I[/TD]
[TD="align: center"]J[/TD]
[/TR]
[TR]
[TD="align: center"]3[/TD]
[TD="colspan: 3, align: center"]Input[/TD]
[TD="align: center"]Total[/TD]
[TD="align: center"]Converted[/TD]
[TD="align: center"]Total[/TD]
[TD="align: center"]Converted[/TD]
[TD="align: center"]G = I?[/TD]
[/TR]
[TR]
[TD="align: center"]4[/TD]
[TD="align: center"]Feet[/TD]
[TD="align: center"]Inches[/TD]
[TD="align: center"]DP[/TD]
[TD="align: center"]Feet[/TD]
[TD="align: center"]Result[/TD]
[TD="align: center"]Inches[/TD]
[TD="align: center"]Result
[/TD]
[TD="align: center"]TRUE[/TD]
[/TR]
[TR]
[TD="align: center"]5[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD]0' 0"[/TD]
[TD="align: center"]0[/TD]
[TD]0' 0"
[/TD]
[TD="align: center"]TRUE[/TD]
[/TR]
[TR]
[TD="align: center"]6[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]2[/TD]
[TD="align: center"]0[/TD]
[TD]0' 0.00"[/TD]
[TD="align: center"]0[/TD]
[TD]0' 0.00"
[/TD]
[TD="align: center"]TRUE[/TD]
[/TR]
[TR]
[TD="align: center"]7[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]4[/TD]
[TD="align: center"]0[/TD]
[TD]0' 0.0000"[/TD]
[TD="align: center"]0[/TD]
[TD]0' 0.0000"[/TD]
[TD="align: center"]TRUE[/TD]
[/TR]
[TR]
[TD="align: center"]8[/TD]
[TD="align: center"]2[/TD]
[TD="align: center"]6[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]2.5[/TD]
[TD]2' 6"[/TD]
[TD="align: center"]30[/TD]
[TD]2' 6"[/TD]
[TD="align: center"]TRUE[/TD]
[/TR]
[TR]
[TD="align: center"]9[/TD]
[TD="align: center"]2[/TD]
[TD="align: center"]6[/TD]
[TD="align: center"]3[/TD]
[TD="align: center"]2.5[/TD]
[TD]2' 6.000"[/TD]
[TD="align: center"]30[/TD]
[TD]2' 6.000"[/TD]
[TD="align: center"]TRUE[/TD]
[/TR]
[TR]
[TD="align: center"]10[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]11.55[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]1.9625[/TD]
[TD]2' 0"[/TD]
[TD="align: center"]23.55[/TD]
[TD]2' 0"[/TD]
[TD="align: center"]TRUE[/TD]
[/TR]
[TR]
[TD="align: center"]11[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]11.555[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]1.962917[/TD]
[TD]1' 11.6"[/TD]
[TD="align: center"]23.555[/TD]
[TD]1' 11.6"[/TD]
[TD="align: center"]TRUE[/TD]
[/TR]
[TR]
[TD="align: center"]12[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]11.555[/TD]
[TD="align: center"]3[/TD]
[TD="align: center"]1.962917[/TD]
[TD]1' 11.555"[/TD]
[TD="align: center"]23.555[/TD]
[TD]1' 11.555"[/TD]
[TD="align: center"]TRUE[/TD]
[/TR]
[TR]
[TD="align: center"]13[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]11.555[/TD]
[TD="align: center"]4[/TD]
[TD="align: center"]1.962917[/TD]
[TD]1' 11.5550"[/TD]
[TD="align: center"]23.555[/TD]
[TD]1' 11.5550"[/TD]
[TD="align: center"]TRUE[/TD]
[/TR]
[TR]
[TD="align: center"]14[/TD]
[TD="align: center"]4[/TD]
[TD="align: center"]11.99999[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]4.999999[/TD]
[TD]5' 0"[/TD]
[TD="align: center"]59.99999[/TD]
[TD]5' 0"[/TD]
[TD="align: center"]TRUE[/TD]
[/TR]
[TR]
[TD="align: center"]15[/TD]
[TD="align: center"]4[/TD]
[TD="align: center"]11.99999[/TD]
[TD="align: center"]2[/TD]
[TD="align: center"]4.999999[/TD]
[TD]5' 0.00"[/TD]
[TD="align: center"]59.99999[/TD]
[TD]5' 0.00"[/TD]
[TD="align: center"]TRUE[/TD]
[/TR]
[TR]
[TD="align: center"]16[/TD]
[TD="align: center"]4[/TD]
[TD="align: center"]11.99999[/TD]
[TD="align: center"]4[/TD]
[TD="align: center"]4.999999[/TD]
[TD]5' 0.0000"[/TD]
[TD="align: center"]59.99999[/TD]
[TD]5' 0.0000"[/TD]
[TD="align: center"]TRUE[/TD]
[/TR]
[TR]
[TD="align: center"]17[/TD]
[TD="align: center"]4[/TD]
[TD="align: center"]11.99999[/TD]
[TD="align: center"]5[/TD]
[TD="align: center"]4.999999[/TD]
[TD]4' 11.99999"[/TD]
[TD="align: center"]59.99999[/TD]
[TD]4' 11.99999"[/TD]
[TD="align: center"]TRUE[/TD]
[/TR]
[TR]
[TD="align: center"]18[/TD]
[TD="align: center"]9[/TD]
[TD="align: center"]0.0001[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]9.000008[/TD]
[TD]9' 0"[/TD]
[TD="align: center"]108.0001[/TD]
[TD]9' 0"[/TD]
[TD="align: center"]TRUE[/TD]
[/TR]
[TR]
[TD="align: center"]19[/TD]
[TD="align: center"]9[/TD]
[TD="align: center"]0.0001[/TD]
[TD="align: center"]3[/TD]
[TD="align: center"]9.000008[/TD]
[TD]9' 0.000"[/TD]
[TD="align: center"]108.0001[/TD]
[TD]9' 0.000"[/TD]
[TD="align: center"]TRUE[/TD]
[/TR]
[TR]
[TD="align: center"]20[/TD]
[TD="align: center"]9[/TD]
[TD="align: center"]0.0001[/TD]
[TD="align: center"]4[/TD]
[TD="align: center"]9.000008[/TD]
[TD]9' 0.0001"[/TD]
[TD="align: center"]108.0001[/TD]
[TD]9' 0.0001"[/TD]
[TD="align: center"]TRUE[/TD]
[/TR]
</tbody>[/TABLE]
Thanks