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

时间:2016-03-26 11:47:34

标签: excel excel-vba excel-formula converter vba

我有大约3600个高度,格式为Feet-Inch-Inch-Eigth一英寸,例如:

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多个,手工工作会很痛苦。如何将此格式转换为英尺和英寸的十进制格式?

2 个答案:

答案 0 :(得分:3)

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

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

并复制下来:

enter image description here

注:

此转换产生十进制英尺,以便 2英尺6英寸 显示 2.5

修改#1:

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

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

答案 1 :(得分:1)

如果你需要走很长的路,那么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

feet2dec