Round Selection - VBA

rilzniak

Active Member
Joined
Jul 20, 2012
Messages
291
Hey everyone,

I'm looking to round a selected range of cells but the code seems to be taking a really long time to run. I'm hoping someone can help me improve what I've written:

Code:
Selection.SpecialCells(xlCellTypeVisible).Select

    For Each cell In Selection
        cell.Value = WorksheetFunction.Round(cell.Value, 2)
        On Error Resume Next
    Next

I've seen other posts related to this before but they were always specific to a range. Is it possible to convert my selection to a non-contiguous range, and then run it on that range or would that run at the same speed? Or is there a better way to perform the task?

Thanks in advance.
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
You can take out the "WorksheetFunction." Round works by itself in VBA (RoundUp/Down does not, however). That might speed it up.

Barring that, how many cells are you trying to work on?
 
Upvote 0
Also, it may help speed up your code if you suppress Calculations and Screen Updating until your loop is finished, i.e.

Place this before your loop:
Code:
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
and then this after:
Code:
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
 
Upvote 0

Forum statistics

Threads
1,223,956
Messages
6,175,612
Members
452,660
Latest member
Zatman

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