• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

macro for rounding values

RAM72

Member
Hi All

Have a summary sheet in which I have a column with numerical values of 2 decimal place.

However I am looking for a macro to automate process through a button to whole numbers as per attached file tp last data row

Thanks for help
 

Attachments

Hi
What i expect 163.5 to read 164

so as example if any value 100.01 to 100.49 then round 100

if 100.50 to 100.99 then round 101

see attached in red with round formula in cells
 

Attachments

Code:
Sub Demo()
    With ActiveSheet
        With .[F2].Resize(.UsedRange.Rows.Count - 1)
             .Formula = "=ROUND(E2,0)"
             .Formula = .Value
        End With
    End With
End Sub
Do you like it ? So thanks to click on bottom right Like !
 
If you wanted to replace the original data instead of add a Column

Code:
Dim c As Range

For Each c In Range("E2:E10")
  c.Value = Application.WorksheetFunction.Round(c, 0)
Next
 
Code:
Sub Demo()
    With ActiveSheet
        With .[F2].Resize(.UsedRange.Rows.Count - 1)
             .Formula = "=ROUND(E2,0)"
             .Formula = .Value
        End With
    End With
End Sub
Do you like it ? So thanks to click on bottom right Like !
Thanks Marc workings as requested :awesome::awesome::):)
 
If you wanted to replace the original data instead of add a Column

Code:
Dim c As Range

For Each c In Range("E2:E10")
  c.Value = Application.WorksheetFunction.Round(c, 0)
Next

Hi Hui

thank you:) working but could you adjust code as long as last data row .
 
Code:
Dim c As Range

For Each c In Range("E2:E" & Range("E" & Rows.Count).End(xlUp).Row)
  c.Value = Application.WorksheetFunction.Round(c, 0)
Next
 
Hi, Mr. hui & Mr. jindon
witch of these code is faster
If I may ask
thank you both
You can test it for yourself...
e.g
Code:
Sub test()
    Dim s As Single
    s = Timer
    With Range("e2", Range("e" & Rows.Count).End(xlUp))
        .Value = Evaluate("index(round(" & .Address & ",0),)")
    End With
    MsgBox Format$(Timer - s, "0.00000")
End Sub
With 95,000 rows
Mine: 0.24219
Other: 5.53125
 
If you want to speed it up try:

Code:
Dim myArr As Variant
myArr = Range("E2:E" & Range("E" & Rows.Count).End(xlUp).Row).Value2
For i = 1 To UBound(myArr, 1)
  myArr(i, 1) = Round(myArr(i, 1))
Next i
Range("E2:E" & Range("E" & Rows.Count).End(xlUp).Row) = myArr

123 Records
Huis 0.03125 sec
Jindon 0.00391 sec
Huis 2 0.00391 sec

95000 Records
Huis 8.89063 sec
Jindon 0.14453 sec
Huis 2 0.07422 sec
 
Hi Hui,
Thanks
Code:
Dim myArr As Variant
myArr = Range("E2:E" & Range("E" & Rows.Count).End(xlUp).Row).Value2
For i = 1 To UBound(myArr, 1)
  myArr(i, 1) = round(myArr(i, 1), 0)
Next i
Range("E2:E" & Range("E" & Rows.Count).End(xlUp).Row) = myArr

End Sub
myArr(i, 1) = Application.WorksheetFunction.round(myArr(i, 1), 0)

Am I correct?
 
Last edited by a moderator:
@mohadin

Using the worksheet function round gives you more control, but uis so much slower than the VBA Round

So myArr(i, 1) = round(myArr(i, 1) )
is much faster than
myArr(i, 1) = Application.WorksheetFunction.round(myArr(i, 1), 0)

Note: The two Round function have a slightly different syntax
 
@mohadin

Using the worksheet function round gives you more control, but uis so much slower than the VBA Round

So myArr(i, 1) = round(myArr(i, 1) )
is much faster than
myArr(i, 1) = Application.WorksheetFunction.round(myArr(i, 1), 0)

Note: The two Round function have a slightly different syntax

but It does not like it
myArr(i, 1) = round(myArr(i, 1) )
I'm using office 2013
 

Attachments

  • Capture.PNG
    Capture.PNG
    24.9 KB · Views: 6
Code:
  myArr(i, 1) = Round(myArr(i, 1))

works ok in Excel 2016

I cannot test in 2013

You can replace Round with
Code:
myArr(i, 1) = Int(myArr(i, 1) + 0.5)
if you want
 
Code:
  myArr(i, 1) = Round(myArr(i, 1))

works ok in Excel 2016

I cannot test in 2013

You can replace Round with
Code:
myArr(i, 1) = Int(myArr(i, 1) + 0.5)
if you want
well,thank you very much in dead
and I'll try to get 2016 on my pc
appreciate
 
Back
Top