1

下面是我创建的一个 VBA 函数,用于在 Excel 中将英尺的测量值从数字转换为文本。例如,该函数将采用数字 1.5 并将其转换为字符串:1'-6"。

但由于某种原因,它错误地舍入了 11.5 英寸的值。当单元格设置为“=11.5/12”(11.5 英寸以英尺表示)时,结果为 0.9583,其中 3 重复。然后当我将此值传递给我的 txtfeet() 函数时,它返回字符串 1'-0"。真正的答案应该是字符串 11 1/2"。

该功能适用​​于其他重复数字,如 9.5 英寸(0.7916 重复 6)。奇怪的是,如果我将它传递 1 英尺和 11.5 英寸(换句话说,1.9583 与 3 重复),它也能正常工作。

任何人都可以帮助解决此错误吗?

这是我的功能的代码。

Public Function txtfeet(FeetIn As Double, Optional Denominator As Integer = 8)

' This function will change a decimal number of feet (FeetIn) to the text string
' representation of feet, inches, and fractional inches.
'
' It will round the fractional inches to the nearest 1/x where x is the denominator
' and is given by the user as the second argument of this function.  If a denominator
' is not given, it defaults to 8 as shown above.
'
' Modified from MrExcel.com

    ' set a variable to the integer portion of FeetIn
    NbrFeet = Fix(FeetIn)

    ' set a variable to the number of inches in decimal form
    InchIn = (FeetIn - NbrFeet) * 12

    ' set a variable to the integer portion of InchIn
    NbrInches = Fix(InchIn)

    ' set a variable to the remaining fractional inches in decimal form
    FracIn = (InchIn - NbrInches) * Denominator

    ' set a variable to be FracIn rounded to the nearest whole number
    Numerator = Application.WorksheetFunction.Round(FracIn, 0)

    ' if the fractional inches are zero, define the portion of text
    ' that will represent the fractional inches to be blank
    If Numerator = 0 Then
        FracText = ""

    ' otherwise, if the number of inches plus the fractional inches rounds up to
    ' 12, add one to NbrFeet and set NbrInches to zero and FracText to blank
    ElseIf (InchIn + (Numerator / Denominator)) = 12 Then
        NbrFeet = NbrFeet + 1
        NbrInches = 0
        FracText = ""

    ' otherwise, if the numerator equals the denominator, add one to NbrInches
    ' and set FracText to blank
    ElseIf Numerator = Denominator Then
        NbrInches = NbrInches + 1
        FracText = ""

    ' otherwise, define FracText in its simplest fractional form
    Else
        ' use a loop to get the fractional inches to their simplest form
        Do
            ' if the numerator is even, divide both numerator and divisor by 2
            If Numerator = Application.WorksheetFunction.Even(Numerator) Then
                Numerator = Numerator / 2
                Denominator = Denominator / 2
            Else
                FracText = " " & Numerator & "/" & Denominator
                Exit Do
            End If
        Loop
    End If

    ' define the output of the function as a text string of feet, inches, and
    ' fractional inches or just inches and fractional inches if NbrFeet is zero
    ' or just fractional inches if both NbrFeet and NbrInches are zero
    If NbrFeet = 0 And NbrInches = 0 And Numerator = 0 Then
        txtfeet = "0"""
    ElseIf NbrFeet = 0 And NbrInches = 0 Then
        txtfeet = Application.WorksheetFunction.Trim(FracText) & """"
    ElseIf NbrFeet = 0 Then
        txtfeet = NbrInches & FracText & """"
    Else
        txtfeet = NbrFeet & "'-" & NbrInches & FracText & """"
    End If

End Function
4

1 回答 1

1

我相信你的台词

ElseIf (InchIn + (Numerator / Denominator)) = 12 Then
    NbrFeet = NbrFeet + 1
    NbrInches = 0
    FracText = ""

应该

ElseIf (NbrInches + (Numerator / Denominator)) = 12 Then
   NbrFeet = NbrFeet + 1
NbrInches = 0
FracText = "
于 2012-10-12T20:55:25.593 回答