Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Round number to 5

I need to round numbers to the nearest 5 in VBA.

ex:

8 - 10
23 - 25
42 - 40

Any suggestions.

Thanks.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 184
Default Round number to 5

Hi, there is probably a more elegant way to do this, but give this a
try:

Sub roundTest()
Dim myRound&
Dim X%
For X = 1 To 3
If WorksheetFunction.RoundUp(Cells(X + 2, 2) / 100, 1) * 100 <= 5 +
Cells(X + 2, 2) Then
myRound = WorksheetFunction.RoundUp(Cells(X + 2, 2) / 100, 1) * 100
Else
myRound = WorksheetFunction.RoundDown(Cells(X + 2, 2) / 100, 1) * 100
End If
Debug.Print Cells(X + 2, 2) & " Rounded to : " & myRound

Next X
End Sub

HTH--Lonnie M.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 184
Default Round number to 5

P.S. my example was using your values starting in cell B3.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 226
Default Round number to 5

I think this should do it

=IF(CEILING(A2,5)-A2<=2.5,CEILING(A2,5),FLOOR(A2,5))

--
Regards

Juan Pablo González

"R-Enemy" wrote in message
...
I need to round numbers to the nearest 5 in VBA.

ex:

8 - 10
23 - 25
42 - 40

Any suggestions.

Thanks.



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 226
Default Round number to 5

Actually, much simpler:

=ROUND(A2/5,0)*5

--
Regards

Juan Pablo González

"Juan Pablo González" wrote in message
...
I think this should do it

=IF(CEILING(A2,5)-A2<=2.5,CEILING(A2,5),FLOOR(A2,5))

--
Regards

Juan Pablo González

"R-Enemy" wrote in message
...
I need to round numbers to the nearest 5 in VBA.

ex:

8 - 10
23 - 25
42 - 40

Any suggestions.

Thanks.







  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Round number to 5

use

Application.round(number,1)

which uses the Excel rounding function rather than

round(number,1)

which uses the VBA rounding function which rounds to the nearest even value.

--
Regards,
Tom Ogilvy

"R-Enemy" wrote in message
...
I need to round numbers to the nearest 5 in VBA.

ex:

8 - 10
23 - 25
42 - 40

Any suggestions.

Thanks.



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 863
Default Round number to 5

The worksheet formula is =ROUND(A1/5,0)*5. You can do the same thing in VBA,
i.e.

Y = Round(X/5,0)*5



On Wed, 9 Feb 2005 08:59:07 -0800, "R-Enemy"
wrote:

I need to round numbers to the nearest 5 in VBA.

ex:

8 - 10
23 - 25
42 - 40

Any suggestions.

Thanks.


  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Round number to 5

On Wed, 09 Feb 2005 15:08:54 -0600, Myrna Larson
wrote:

The worksheet formula is =ROUND(A1/5,0)*5. You can do the same thing in VBA,
i.e.

Y = Round(X/5,0)*5


In more recent versions of VBA, one does not get the same answer. Try 12.5:
VBA Round -- 10
worksheet -- 15


--ron
  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 863
Default Round number to 5

I think that's true in ALL versions of VBA. It uses Banker's Rounding.

On Wed, 09 Feb 2005 17:13:08 -0500, Ron Rosenfeld
wrote:

On Wed, 09 Feb 2005 15:08:54 -0600, Myrna Larson
wrote:

The worksheet formula is =ROUND(A1/5,0)*5. You can do the same thing in VBA,
i.e.

Y = Round(X/5,0)*5


In more recent versions of VBA, one does not get the same answer. Try 12.5:
VBA Round -- 10
worksheet -- 15


--ron


  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default Round number to 5

Only VBA6+. VBA5 (XL97, MacXL) doesn't have the ROUND() function.




In article ,
Myrna Larson wrote:

I think that's true in ALL versions of VBA. It uses Banker's Rounding.



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Round number to the thousands without changing underlying number Tim Caldwell Excel Discussion (Misc queries) 3 June 13th 07 09:37 PM
Excel. How to round a number to nearest half number? HaraldS Excel Discussion (Misc queries) 2 February 19th 07 09:50 AM
Can I increase a number by 5.5% and then round that number to the. Jeff Thornburg Excel Discussion (Misc queries) 1 June 28th 06 05:26 PM
How can i round a number to closest tenth number? rayne95 Excel Worksheet Functions 3 June 19th 06 09:34 PM
How to make a number round up/down to a set number David S Excel Worksheet Functions 1 April 7th 05 04:20 PM


All times are GMT +1. The time now is 09:58 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"