VBA Code to roundup a number

Darrell MacDonald

New Member
Joined
Mar 25, 2021
Messages
14
Office Version
  1. 2010
Platform
  1. Windows
Hello
I'm hoping someone can provide me with code that will round up the value in each cell in a column of cells. For example, could it take a cell with "23,937,012" and make it be "=roundup(23,937,012,-5)", then bump down to the next cell, do the same with that particular number and continue down to the last number with a value in it?

Thanks in advance.
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
This sample code will take the used range in column A (A1:A???) and perform the round up for each cell, then replace A1:A??? with the new values.
VBA Code:
Sub DMac()
Dim arr() As Variant, i As Long, lastrow As Long
lastrow = Range("A" & Rows.Count).End(xlUp).Row
arr = Range("A1:A" & lastrow)

For i = 1 To UBound(arr)
    arr(i, 1) = WorksheetFunction.RoundUp(arr(i, 1), -5)
Next i

Range("A1").Resize(UBound(arr)).Value = arr
End Sub
 
Upvote 0
Z51
Thanks so much for taking the time to write that code. Is there a way to make it work on a chosen column of values? This version works beautifully if the range is from A1:?.
 
Upvote 0
Fluff
There are several columns that I'd like this run on. Could I click on the top cell and have the routine start there and do what Z51's code currently does to column A? Then, I'd click on a few more. Here's a screenshot of the columns.
1668426706756.png
 
Upvote 0
Ok, how about
Excel Formula:
Sub Darrell()
   With Range(ActiveCell, Cells(Rows.Count, ActiveCell.Column).End(xlUp))
      .Value = Evaluate(Replace("if(@="""","""",if(istext(@),@,roundup(@,-5)))", "@", .Address))
   End With
End Sub
 
Upvote 0
Solution
Wow! Fluff, that's awesome!! Thanks so much to both you and Z51. I'm always impressed by the kindness of this board's members.
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0
Fluff. Sorry to be a pain but there's one thing I just noticed and didn't mention to you earlier. Each of the cells contains a formula and isn't just a number. For example, one of them is:
='PP-6 400'!$M$278

The current code removes the formula and turns it into an integer. Is there a way to allow the formula to remain like so: =roundup('PP-6 400'!$M$278,-5)
 
Upvote 0
That is a completely different thing, so it needs a new thread.
 
Upvote 0

Forum statistics

Threads
1,215,025
Messages
6,122,732
Members
449,093
Latest member
Mnur

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top