Free Excel macros

Add ROUND

Applies the ROUND function to every number and formula in your selection, so your figures are rounded consistently without editing each cell.

This macro uses Turn Stuff Off/On, so add those two to the same module first.

VBA: Add ROUND Function

Sub Add_ROUND() 'This macro is used to round numeric cell values to a user provided number of decimal places; obtained from bgrconsulting.co.uk/macros where help and support can be found.
Dim oRng As Range, oRngC As Range, oRngF As Range, nRng As Range

On Error Resume Next
If Selection.Cells.Count = 1 Then
    Set oRng = Selection
Else
    Set oRng = Selection.SpecialCells(xlCellTypeVisible)
    Set oRngC = oRng.SpecialCells(xlCellTypeConstants)
    Set oRngF = oRng.SpecialCells(xlCellTypeFormulas)
    If oRngC Is Nothing Or oRngF Is Nothing Then
        If oRngC Is Nothing Then Set oRng = oRngF
        If oRngF Is Nothing Then Set oRng = oRngC
    Else
        Set oRng = Union(oRngC, oRngF)
    End If
End If

For Each Cell In oRng
    If IsNumeric(Cell.Value) Then
        If nRng Is Nothing Then Set nRng = Cell Else Set nRng = Union(nRng, Cell)
    End If
Next Cell
If nRng Is Nothing Then MsgBox "No cells in your selection contain numeric values; nothing happened.", vbOKOnly, "Try again:": Exit Sub

Begin:
dps = InputBox("To how many decimal places would you like to round the selected cells?", "Decimal Places?")
If dps = "" Then Exit Sub
On Error GoTo -1: On Error GoTo Leave
dps = Int(dps)
On Error GoTo 0

If MidSub <> vbYes Then Call TurnStuffOff
    For Each Cell In nRng
        On Error GoTo -1: On Error GoTo NextCell
        If Cell.Value = "" Then GoTo NextCell
        
        If Cell.Style = "Percent" Then
            AnsP = MsgBox("Cell " & Cell.Address(0, 0) & " is formatted as a percentage, would you like to round this number to " & dps + 2 & " decimal places instead?", vbExclamation + vbYesNo, "Warning: Percentage Detected!")
            If AnsP = vbYes Then dps = dps + 2
            If AnsP = vbNo Then Skip = MsgBox("Would you like to skip the rounding of cell " & Cell.Address(0, 0) & "?", vbExclamation + vbYesNo, "Warning: Percentage Detected!")
            If Skip = vbYes Then GoTo NextCell
        End If
        
        If Left(Cell.Formula, 1) <> "=" Then Cell.Value = Round(Cell.Value, dps): GoTo NextCell
        
        If InStr(Cell.Formula, "=ROUND(ROUND(") Then Cell.Formula = "=ROUND(" & Mid(Cell.Formula, 14, Len(Cell.Formula) - 19) & "," & dps & ")": GoTo NextCell
        
        If InStr(Cell.Formula, "=ROUND(") And Mid(Cell.Formula, Len(Cell.Formula) - 1, 1) = "" & dps & "" Then GoTo NextCell
        
        If Left(Cell.Formula, 7) = "=ROUND(" Then _
            Rounded = MsgBox("Cell " & Cell.Address(0, 0) & " is already rounded as follows:" _
            & vbNewLine & Cell.Formula & vbNewLine _
            & vbNewLine & "Would you like to replace this formula with the following:" _
            & vbNewLine & "=ROUND(" & Mid(Cell.Formula, 8, Len(Cell.Formula) - 10) & "," & dps & ")", _
            vbExclamation + vbYesNo, "Update Rounding?")
        If Rounded = vbNo Then GoTo NextCell
        If Rounded = vbYes Then Cell.Formula = "=ROUND(" & Mid(Cell.Formula, 8, Len(Cell.Formula) - 10) & "," & dps & ")": GoTo NextCell
        
        Cell.Formula = "=ROUND(" & Mid(Cell.Formula, 2, 9999) & "," & dps & ")"
        
NextCell:
        If AnsP = vbYes Then dps = dps - 2
        AnsP = Empty
        Rounded = Empty
    
    Next Cell

If MidSub <> vbYes Then MsgBox "All visible cells within the selection have been rounded.", vbOKOnly, "Success!": Call TurnStuffOn
Exit Sub

Leave:
Retry = MsgBox("The value entered must be an integer (between 0 and 22).  Would you like to try again?", vbCritical + vbYesNo, "Retry?")
If Retry = vbYes Then GoTo Begin
If MidSub <> vbYes Then Call TurnStuffOn
End Sub

New to macros? Set up your Personal Macro Workbook first, then paste this code into its module.