View Single Post
  #11   Report Post  
Posted to microsoft.public.excel.programming
Ron Rosenfeld Ron Rosenfeld is offline
external usenet poster
 
Posts: 5,651
Default VB Round( ) function

On Wed, 12 Nov 2003 12:22:52 -0000, "Andrew" wrote:

If in a cell on a spreadsheet, I enter the following equation:
=ROUND(50.5, 0)
I obtain the result 51, which I would expect, since 0.5 should be rounded
up.

If I write the following VB funcion:

Function RoundFunc(Mark As Integer) As Integer
RoundFunc = Round(Mark, 0)
End Function

And then enter the following equation into a cell:
=RoundFunc(50.5)

I obtain the result 50. i.e. 0.5 is rounded down

Why the difference? Is there a function that I can use in my VB code that
will round 0.5 up rather than down?


The difference is because the VBA Round function uses a different algorithms,
and 0.5 will be rounded up or down depending on whether the preceding number is
odd or even.

The equivalent VBA function, to round like the worksheet:

Application.Worksheetfunction.Round(n,digits)


--ron