Be the first user to complete this post

  • 0
Add to List

Excel-VBA: Math Functions – ROUND()

Description: 

The ROUND function in MS excel returns a rounded number based on the specified number of digits.

Note : ROUND() works slightly diffrent when used as Excel formula, Click here to know about it Formula-ROUND()

Format:

As VBA Function : ROUND(number, [decimal places])

Note : If you don't provide the decimal places the function will consider it as 0 and returns the Integer.

Arguments:
  • Number
    • Mandatory
    • Type: Number
    • Number which requires to be rounded.
  • decimal places
    • Optional
    • Type: Number
    • Number of digits to the number to

Important Notes:

If number you are rounding ends with 5 then this function will return the number ending with nearest even number, see example

Round(4.55,1)
Output: 4.6   (round up)

Round(6.85,1)
Output: 6.8 (round down)

Other Examples:

Round(3.33,0)
Output: 3

Round(25.77,1)
Output: 25.8

Round(37.14,1)
Output: 37.1

Round(26.32)
Output: 26

Code:

Function getROUND()

Dim val1

Dim val2

Dim val3

Dim val4

val1 = 3.33

val2 = 8.85

val3 = 9.99

val4 = 25.787676

strResult = "The ROUND number of " & val1 & " when no argument for decimal provided " & Round(val1) & vbCrLf

   strResult = strResult & "The ROUND number of " & val2 & " with 1 decimal place is " & Round(val2, 1) & vbCrLf

   strResult = strResult & "The ROUND number of " & val3 & " with 0 decimal place is " & Round(val3, 0) & vbCrLf

   strResult = strResult & "The ROUND number of " & val4 & " with 2 decimal place is " & Round(val4, 2)

MsgBox strResult

End Function

Output:

ROUND() - Function
ROUND() - Function