VBA Abs Function

VBA Abs Function

Overview of VBA Abs Function:


VBA Abs function is categorized as Math(Mathematical) & Trig function. This is a built-in Excel VBA Function. This function returns or calculates an absolute value of a number. This function removes negative sign in front of number. The Abs function converts negative value to positive value.

We can use this function in Excel and VBA. This function can be used in either procedure or function in a VBA editor window in Excel. We can use this VBA Abs Function in any number of times in any number of procedures or functions. Let us learn what is the syntax and parameters of the Abs function, where we can use this Abs Function and real-time examples in Excel VBA.

Syntax of VBA Abs Function

The syntax of the Abs Function in VBA is

Abs(Number)

The Abs function returns a numeric value.

Parameters or Arguments:

The Abs function has one argument in Excel VBA.
where
Number:The Number is a required parameter. It represents a number or numeric value. We use this parameter to calculate absolute value.
Note:

  • The specified value is not a number, it returns an error (Run time: Type mismatch error)
  • The specified number has negative sign, it removes negative sign and returns positive value.
  • The specified number is Null, then it returns Null.
  • The specified number is zero(0), then it returns zero(0).

Where we can apply or use VBA Abs Function?

We can use this Abs Function in VBA MS Office 365, MS Excel 2016, MS Excel 2013, 2011, Excel 2010, Excel 2007, Excel 2003, Excel 2016 for Mac, Excel 2011 for Mac, Excel Online, Excel for iPhone, Excel for iPad, Excel for Android tablets and Excel for Android Mobiles.

Example 1: Convert a value(-4) to an absolute number

Here is a simple example of the VBA Abs function. This below example converts a value(-4) to an absolute number. The below example removes negative sign and returns an output as 4.

'Convert a value(-4) to an absolute number
Sub VBA_Abs_Function_Ex1()

    'Variable declaration
    Dim iValue As Integer
    Dim vResult As Variant
    
    iValue = -4
        
    vResult = Abs(iValue)
        
    MsgBox "Convert a number(-4) to an absolute value : " & vResult, vbInformation, "VBA Abs Function"
    
End Sub

Output: Here is the screen shot of the first example output.
VBA Abs Function

Example 2: Convert a value(4) to an absolute number

Here is a simple example of the VBA Abs function. This below example converts a value(4) to an absolute number. The below macro code returns an output as 4.

'Convert a value(4) to an absolute number
Sub VBA_Abs_Function_Ex2()

    'Variable declaration
    Dim iValue As Integer
    Dim vResult As Variant
    
    iValue = 4
        
    vResult = Abs(iValue)
        
    MsgBox "Convert a number(4) to an absolute value : " & vResult, vbInformation, "VBA Abs Function"
    
End Sub

Output: Here is the screen shot of the second example output.
VBA Abs Function

Example 3: Convert a value(0) to an absolute number

Here is a simple example of the VBA Abs function. This below example converts a value(0) to an absolute number. The below macro code returns an output as 0.

'Convert a value(0) to an absolute number
Sub VBA_Abs_Function_Ex3()

    'Variable declaration
    Dim iValue As Integer
    Dim vResult As Variant
    
    iValue = 0
        
    vResult = Abs(iValue)
        
    MsgBox "Convert a number(0) to an absolute value : " & vResult, vbInformation, "VBA Abs Function"
    
End Sub

Output: Here is the screen shot of the third example output.
VBA Abs Function

Example 4: Convert a string(Text) to an absolute number

Here is a simple example of the VBA Abs function. This below example converts a string(Text) to an absolute number. The below macro code returns an error. The Abs function converts only numeric values not string.

'Convert a value("Text") to an absolute number
Sub VBA_Abs_Function_Ex4()

    'Variable declaration
    Dim sValue As String
    Dim vResult As Variant
    
    sValue = "Text"
        
    vResult = Abs(sValue)
        
    MsgBox "Convert a string('Text') to an absolute value : " & vResult, vbInformation, "VBA Abs Function"
    
End Sub

Output: Here is the screen shot of the fourth example output.
VBA Runtime Type mismatch Error

Example 5: Convert a value(Null) to an absolute number

Here is a simple example of the VBA Abs function. This below example converts a value(Null) to an absolute number. This function returns an output as ‘Null’.

'Convert a value(Null) to an absolute number
Sub VBA_Abs_Function_Ex5()

    'Variable declaration
    Dim iValue
    Dim vResult As Variant
    
    iValue = Null
        
    vResult = Abs(iValue)
        
    MsgBox "Convert 'Null' to an absolute value : " & vResult, vbInformation, "VBA Abs Function"
    
End Sub

Output: Here is the screen shot of the fifth example output.
VBA Abs Function

Instructions to use Macro Codes

Here are the step by step instructions to use the VBA Abs function in Excel VBA.

  • Open an Excel workbook.
  • Press Alt+F11 to open VBA Editor window.
  • Go to Insert menu and click on module from the available options.
  • Copy above specified macros to the VBA editor.
  • Select any procedure from above codes and click on ‘Run’ command or use Keyboard shortcut ‘F5’.

List of VBA Functions:

Please click on below link to go back to see all List of VBA Functions.

VBA Math(Mathematical) & Trig Functions List of VBA Functions

Leave a Comment

Your email address will not be published. Required fields are marked *