Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将TRUNC函数用于Excel中的VBA_Excel_Vba - Fatal编程技术网

如何将TRUNC函数用于Excel中的VBA

如何将TRUNC函数用于Excel中的VBA,excel,vba,Excel,Vba,我需要使用TRUNC函数,这是在Excel中提供的,到VBA。我注意到它在Application.WorksheetFunction中不可用。我怎样才能得到它 提前感谢。您可以使用拆分功能并在上拆分 NewStr = split(string, ".")(0) 如果您想使用TRUNC而不是Fix,因为您需要指定截断后剩余的小数位数,您可以使用Application.Evaluate执行以下操作: Debug.Print Application.Evaluate("=TRUNC(3.141592

我需要使用TRUNC函数,这是在Excel中提供的,到VBA。我注意到它在Application.WorksheetFunction中不可用。我怎样才能得到它


提前感谢。

您可以使用拆分功能并在
上拆分

NewStr = split(string, ".")(0)

如果您想使用
TRUNC
而不是
Fix
,因为您需要指定截断后剩余的小数位数,您可以使用
Application.Evaluate
执行以下操作:

Debug.Print Application.Evaluate("=TRUNC(3.14159265359, 4)")
'will return 3.1415

如果需要VBA等效项,请执行以下操作:

Function vbaTRUNC(num As Double, Optional digits As Long = 0) As Double
    vbaTRUNC = Fix(num * 10 ^ digits) / 10 ^ digits
End Function

你挑吧。以下所有函数返回
2.71828182845905的
2

公共子测试TT()


End Sub

@BigBen根据您的来源,此函数在我的Excel API版本中不可用。我会设法解决的。谢谢。我建议您使用
Fix
。或者使用
WorksheetFunction.Floor()
请显示您试图在中使用的
TRUNC()
代码。这是一个字符串-您需要
CLng
。这仍然是过度的<代码>修复
就是这么做的。这就是我最后做的。我只是想要一些本地的东西,但没有办法。很乐意帮忙。如果我的回答符合你的要求,请注明接受。看见
Dim x As Double
x = Exp(1)

Debug.Print x, WorksheetFunction.Floor(x, 1)
Debug.Print x, Fix(x)
Debug.Print x, CLng(x)
Debug.Print x, WorksheetFunction.Round(x - 0.5, 0)