Round Selection - VBA

rilzniak

Active Member
Joined
Jul 20, 2012
Messages
288
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

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
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
Also, it may help speed up your code if you suppress Calculations and Screen Updating until your loop is finished,
Thanks. I usually do that with my code but didn't in this instance, for no good reason.
 
Upvote 0

Forum statistics

Threads
1,215,332
Messages
6,124,314
Members
449,153
Latest member
JazzSingerNL

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