将英尺和英寸转换为十进制格式

Convert feet and inches to decimal format

我有大约 3600 个高度,格式为英尺-英寸-英寸-八分之一英寸,例如:

5103 = 5 feet, 10 and 3/8 inches
6000 = 6 feet
6022 = 6 feet, 2 and 2/8 inches

我需要使用公式或宏将它们转换为十进制格式,以便这些结果为:

5103 = 5.864
6000 = 6.000
6022 = 6.187

因为我有 3600 多个,手动工作会很痛苦。如何将此格式转换为英尺和英寸的十进制格式?

使用列 A 中的数据,在 B1 中输入:

=--LEFT(A1,1)+MID(A1,2,2)/12+RIGHT(A1,1)/(12*8)

并抄下来:

注意:

此转换产生小数英尺,因此 2 英尺 6 英寸 显示 2.5

编辑#1:

如果您需要在 A 列中容纳超过 9 英尺,则公式应为:

=--LEFT(A1,LEN(A1)-3)+MID(A1,LEN(A1)-2,2)/12+RIGHT(A1,1)/(12*8)

如果您需要走很长的路,VBA UDF 似乎是最方便的。

Function feet2dec(str As String) As Double
    Dim v As Long, vtmp As Variant, tmp As String
    Dim feet As Double

    vtmp = Split(str, Chr(44))
    For v = LBound(vtmp) To UBound(vtmp)
        Select Case LCase(Right(Trim(vtmp(v)), 4))
            Case "feet"
                feet = feet + Val(Trim(Replace(vtmp(v), "feet", vbNullString, 1, -1, vbTextCompare)))
            Case "ches"
                str = Replace(vtmp(v), "inches", vbNullString, 1, -1, vbTextCompare)
                str = Application.Trim(Replace(str, "and", vbNullString, 1, -1, vbTextCompare))
                feet = feet + Application.Evaluate(str) / 12
        End Select
    Next v

    feet2dec = feet
End Function