Ceiling function

I

iccsi

I would like to have a function return an integer which give me
ceiling result.
For example, I need get result "1", for any number from 0.001 to 0.999
and get result "2" for any number from 1.001 to 1.999

I tried to use Int, Round which trunc the number after decimal.

Your help is great appreciated,
 
A

Arvin Meyer

One of the beauties of Office automation is that you can use functions from
other Office products. While I could write custom code for an Access Ceiling
function, I prefer to use Excel's, like this:

Public Function XLCeiling(dblNum As Double, dblUp As Double) As Double
Dim objXL As Object
Set objXL = CreateObject("Excel.Application")
XLCeiling = objXL.WorksheetFunction.Ceiling(dblNum, dblUp)
End Function

--
Arvin Meyer, MCP, MVP
http://www.datastrat.com
http://www.accessmvp.com
http://www.mvps.org/access
Co-author: "Access 2010 Solutions", published by Wiley
 
J

John W. Vinson

I would like to have a function return an integer which give me
ceiling result.
For example, I need get result "1", for any number from 0.001 to 0.999
and get result "2" for any number from 1.001 to 1.999

I tried to use Int, Round which trunc the number after decimal.

Your help is great appreciated,

The Int() function rounds down, even for negative numbers - so negate your
number, use int, and negate the result:

-Int(-[yournumberfield])
 
M

Marshall Barton

iccsi said:
I would like to have a function return an integer which give me
ceiling result.
For example, I need get result "1", for any number from 0.001 to 0.999
and get result "2" for any number from 1.001 to 1.999

I tried to use Int, Round which trunc the number after decimal.


As an opposing view, I try to avoid loading other
applications if I can get what I need using a simple custom
function.

Public Function Ceiling(x)
Ceiling = -Int(-x)
End Function
 
D

David W. Fenton

One of the beauties of Office automation is that you can use
functions from other Office products. While I could write custom
code for an Access Ceiling function, I prefer to use Excel's, like
this:

Public Function XLCeiling(dblNum As Double, dblUp As Double) As
Double Dim objXL As Object
Set objXL = CreateObject("Excel.Application")
XLCeiling = objXL.WorksheetFunction.Ceiling(dblNum, dblUp)
End Function

I'd suggest using a static variable inside that function, and
checking if it Is Nothing, and only then initializating it. That
way, you won't be starting up Excel every time you call it (and
you're not shutting it down, so you'd end up with a bunch of
orphaned invisible instances of Excel with your code, no?). The code
should probably also have an optional flag to tear down Excel, so
I'd probably write it something like this (though it needs error
handling):

Public Function XLCeiling(ByVal dblNum As Double, _
ByVal dblUp As Double, _
ByVal Optional bolClose As Boolean) As Double
Static objXL As Object

If bolClose Then
Set objXL = Nothing
Exit Function
End If
If objXL Is Nothing Then
Set objXL = CreateObject("Excel.Application")
End If
XLCeiling = objXL.WorksheetFunction.Ceiling(dblNum, dblUp)
End Function
 
D

David W. Fenton

Before going through all that, I'd use the solution Marsh Barton
posted. It's more elegant than opening Excel anyway.

Certainly for this particular function. But for more complex Excel
functions, automating Excel is better since you are guaranteed of
the same result, rather than running the risk of implementing
something slightly different in Access.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top