0

Using Excel Macros:

I am trying to create a function that takes a double called Feet and another called Inches and have it return the total number of inches. That is the easy part. I want it to then display the information in the cell like so:

5ft 8in

Here is the code that I have come up with:

Function FTIN(Feet As Double, Inches As Double) As Double
    'This part works fine.
    Dim total As Double

    total = (Feet * 12) + Inches

    FTIN = total

    'this part does not seem to work.
    Dim foot As Double
    Dim inch As Double

    foot = WorksheetFunction.RoundDown(total / 12, 0)
    inch = total - (foot * 12)

    result = foot & "ft " & inch & "in"
End Function

while it does return the the total number of inches it does not display it in the way I need it to.

I basically want it to do the same thing as dates where the value is 41361 but what is displayed is "3/28/2013".

Also I know I can make the value as 508 and I can make the custom format as 0"ft "00"in", but that would not give me a value that I can work with.

Please help me with this.

4

2 回答 2

0

我认为您不能以您想要的方式自定义格式。我建议制作第二个返回字符串的函数。将英尺和英寸插入这个新函数,为“ft”和“in”添加文本,然后将它们连接在一起(使用 & 运算符)。

于 2013-03-29T20:08:33.470 回答
0

你可以用工作表公式做到这一点,试试这个:

=INT(a1/12)&"ft  "&TRIM(TEXT(a1-INT(a1/12)*12,"# ???/???"))&"in"

其中 A1 是其中包含您的 UDF 的单元格。

于 2015-11-06T16:04:03.573 回答