Excel VBA To Round to 2 Decimal Places: 5 Methods

Method 1 – Using VBA to Round a Number to 2 Decimal Places for a Specific Sheet

STEPS:

  • Go to the Developer tab >> select Visual Basic.

  • VBA window will appear. Go to Insert >> select Module.

excel vba round to 2 decimal places

  • In the module, write down the following code.
Sub Round_Numbers()
Dim ws As Worksheet
Set ws = Worksheets("Round A Number")
ws.Range("C4") = Application.WorksheetFunction.Round(ws.Range("B4"), 2)
End Sub

Code Breakdown

  • Created a Sub Procedure Round_Numbers. Used the dim statement to define a variable ws as Worksheet.
  • Used the Set statement to set the worksheet named “Round A Number” using the Worksheets object in ws
  • Used Round to get the number in B4 rounded. The result will be in C4 and 2, which defines that Excel will round the number to 2 decimal places.
  • Press F5 to run the code. Excel will return the output in C4.

excel vba round to 2 decimal places


Method 2- Applying VBA Macros to Round a Cell Value

STEPS:

  • Press ALT + F11 to open the VBA macros window.
  • Go to Insert >> select Module.

  • In the module, write down the following code.
Sub Round_Cell_Value()
Worksheets("Round A Cell").Cells(4, 3).Value = _
Application.WorksheetFunction.Round(Cells(4, 2).Value, 2)
End Sub

excel vba round to 2 decimal places

Code Breakdown

  • Created a Sub Procedure Round_Cell_Value.
  • Used the Worksheet object to mention the Sheet name Cell Value where the Sheet name is “Round A Cell” and the Cell Value (4,3) is C4 to keep my output.
  • Used Round to get the number in B4 rounded. The result will be in C4 and 2, which defines that Excel will round the number to 2 decimal places.
  • Run the code by selecting the icon (see image).

Excel will return the output.

excel vba round to 2 decimal places


Method 3 – Using VBA to Round a Range of Numbers to 2 Decimal Places

Go for something a bit more complex and useful. Use VBA macros to round a range of numbers.
We have the Amount of Tax for some people (in C4:C9), and want the numbers to be rounded.

STEPS:

  • Open a module in the VBA macros window following method-1 or method-2.
  • In the module, write down the following code.
Sub Round_a_Range()
    Dim n As Integer
    Dim ws As Worksheet
    Set ws = Worksheets("Round A Range")
    For n = 4 To 9
        ws.Cells(n, 4).Value = Application.WorksheetFunction.Round(Cells(n, 3).Value, 2)
    Next n
End Sub

excel vba round to 2 decimal places

Code Breakdown

  • Created a Sub Procedure Round_a_range. I used the dim statement to define a variable ws as Worksheet.
  • Used the Set statement to set the worksheet named “Round A Range” using the Worksheets object in ws
  • Took a variable n as an integer and applied a For loop for n which will go from the 4th to 9th row.
  • Used Round to get the numbers rounded.
  • Press F5 to run the code. Excel will return the output.

  • Format as you wish.

excel vba round to 2 decimal places


Method 4 – Inserting MsgBox to Show Rounded Number

STEPS:

  • Open a module in the VBA macros window following method-1 or method-2.
  • In the module, write down the following code.
Sub Using_MsgBox()
Dim Number As Double
Number = 257.865
MsgBox "The value is " & Round(Number, 2)
End Sub

Code Breakdown

  • Created a Sub Procedure Using_Msgbox. Then I used the dim statement to define a variable Number as Double and placed 865 in the Number variable.
  • Inserted a MsgBox, which will show me the rounded number.
  • Press F5 to run the code. Excel will return the output.

excel vba round to 2 decimal places


Method 5 – Use of Cell Reference to Round a Number in Excel

STEPS:

  • Open a module in the VBA macros window following method-1 or method-2.
  • In the module, write down the following code.
Sub Using_Cell_Reference()
Dim Round_2_Decimal As Double
Round_2_Decimal = Round(Range("B4").Value, 2)
MsgBox "The value is " & Round_2_Decimal
End Sub

Code Breakdown

  • Created a Sub Procedure Using_Cell_Reference. Used the dim statement to define a variable Round_2_Decimal as Double.
  • Round_2_Decimal will hold the rounded value of the number in the B4 cell upto 2 decimal places.
  • Inserted a MsgBox which will show me the rounded number.
  • Press F5 to run the code. Excel will return the output.

excel vba round to 2 decimal places


Download Workbook


Related Articles


<< Go Back to Round Up Decimals | Rounding in Excel | Number Format | Learn Excel

Get FREE Advanced Excel Exercises with Solutions!
Akib Bin Rashid
Akib Bin Rashid

AKIB BIN RASHID, a materials and metallurgical engineer, is passionate about delving into Excel and VBA programming. To him, programming is a valuable time-saving tool for managing data, files, and internet-related tasks. Proficient in MS Office, AutoCAD, Excel, and VBA, he goes beyond the fundamentals. Holding a B.Sc in Materials and Metallurgical Engineering from Bangladesh University of Engineering and Technology, MD AKIB has transitioned into a content development role. Specializing in creating technical content centred around Excel and... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo