vba Access 中的天花板功能

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1776001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 11:01:55  来源:igfitidea点击:

Ceiling function in Access

excelms-accessmathvbams-office

提问by Ronnie Overby

Have searched for this, no luck.

已经搜索过这个,没有运气。

Can someone tell me how create a Ceiling Function in MS access that behaves the same as the one in excel?

有人能告诉我如何在 MS 访问中创建一个与 Excel 中的相同的天花板函数吗?

采纳答案by marg

You can add a Reference to the Microsoft Excel Object Library and use Excel.WorksheetFunction.Ceiling

您可以添加对 Microsoft Excel 对象库的引用并使用 Excel.WorksheetFunction.Ceiling

回答by MiloNC

Since Int() seems to work like Floor(), you can get Ceiling like this: -Int(-x)

由于 Int() 似乎像 Floor() 一样工作,您可以像这样获得天花板:-Int(-x)

回答by Arthur Frankel

This answer uses VBA for Access, and is derived from http://www.tek-tips.com/faqs.cfm?fid=5031:

此答案使用 VBA 进行访问,并源自http://www.tek-tips.com/faqs.cfm?fid=5031

Public Function Ceiling(ByVal X As Double, Optional ByVal Factor As Double = 1) As Double
    ' X is the value you want to round
    ' Factor is the optional multiple to which you want to round, defaulting to 1
    Ceiling = (Int(X / Factor) - (X / Factor - Int(X / Factor) > 0)) * Factor
End Function

Note that this answer is mathematically correct for negative X. See http://en.wikipedia.org/wiki/Floor_and_ceiling_functions#Spreadsheet_softwarefor background.

请注意,此答案对于负 X 在数学上是正确的。有关背景信息,请参阅http://en.wikipedia.org/wiki/Floor_and_ceiling_functions#Spreadsheet_software

回答by Ronnie Overby

Thanks, marg, for the answer. For future reference, here is the VBA function that I wrote after importing the Microsoft Excel Object Library:

谢谢玛格的回答。为了将来参考,这里是我在导入 Microsoft Excel 对象库后编写的 VBA 函数:

Public Function Ceiling(Value As Double, Significance As Double) As Double
    Ceiling = Excel.WorksheetFunction.Ceiling(Value, Significance)
End Function

Then in my query, I was trying to calculate billable hours from actual time worked, rounding up to the next quarter hour:

然后在我的查询中,我试图从实际工作时间计算计费小时数,四舍五入到下一个刻钟:

SELECT Ceiling(([WorkTimes]![EndTime]-[WorkTimes]![BeginTime])*24,0.25) AS BillableTime
FROM WorkTimes;

回答by Tim Murphy

While this question specifically asked for Access here is the answer for VB.NET

虽然这里专门针对 Access 提出的这个问题是 VB.NET 的答案

Public Function Ceiling(ByVal value As Double, ByVal factor As Double) As Double
    Return Math.Ceiling(value / factor) * factor
End Function

And the answer in C#

和 C# 中的答案

public double Ceiling(double value, double factor)
{
    return Math.Ceiling(value / factor) * factor;
}

I'm posting it here because I needed such a function google sent me to this question but I couldn't find an answer for .Net. I finally figured it out for myself.

我在这里发布它是因为我需要这样一个功能 google 给我发送了这个问题,但我找不到 .Net 的答案。我终于自己想通了。