VBA STR Function

VBA STR Function

Overview of VBA Str Function:

VBA Str function is categorized as a Text/String function in VBA. It is a built-in function in MS Office Excel. It returns a string representation of a number. It has one mandatory number parameter. If input value is Null, then it returns an error. It could be used only as a VBA function. We can not use this function in Excel. The Str function can be used in either procedure or function in a VBA editor window in Excel. We can use this VBA Str function any number of times in any number of procedures or functions. In the following section we learn what is the syntax and parameters of the Str function, where we can use this Str function and real-time examples in VBA.

Table of Contents:

Syntax of VBA Str Function

The syntax of the VBA Str function is

Str(Number)

Note: This Str function returns a number with string type.

Parameters or Arguments

This function has one mandatory parameter or argument for the Str Function.
Where
Number:The number is a mandatory argument. It represents a number with string data type.

Where we can apply or use the VBA Str Function?

We can use this VBA Str function in 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 number(5) to a string

Here is a simple example of the VBA Str function. This below example macro returns a string. It contain a value of 5.

'Convert number(5) to a string
Sub VBA_Str_Function_Ex1()

    Dim iInput As Integer, sOutput As String
    
    iInput = 5
    
    sOutput = Str(iInput)
        
    MsgBox "Converting number to a string : " & sOutput, vbInformation, "VBA Str Function"

End Sub

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

Example 2: Convert number(0) to a string

Here is a simple example of the VBA Str function. This below example macro returns a string. It contain a value of 0.

'Convert number(0) to a string
Sub VBA_Str_Function_Ex2()

    Dim iInput As Integer, sOutput As String
    
    iInput = 0
    
    sOutput = Str(iInput)
        
    MsgBox "Converting number to a string : " & sOutput, vbInformation, "VBA Str Function"

End Sub

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

Example 3: Convert number(-5) to a string

Here is a simple example of the VBA Str function. This below example macro returns a string. It contain a value of -5.

'Convert number(-5) to a string
Sub VBA_Str_Function_Ex3()

    Dim iInput As Integer, sOutput As String
    
    iInput = -5
    
    sOutput = Str(iInput)
        
    MsgBox "Converting number to a string : " & sOutput, vbInformation, "VBA Str Function"

End Sub

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

Example 4: If Str function input number is null

Here is a simple example of the VBA Str function. This below example macro returns an error.

'Convert number(Null) to a string
Sub VBA_Str_Function_Ex4()

    Dim iInput As Integer, sOutput As String
    
    iInput = Null
    
    sOutput = Str(iInput)
        
    MsgBox "Converting number to a string : " & sOutput, vbInformation, "VBA Str Function"

End Sub

Output: Here is the screen shot of the fourth example output.
VBA Str Function

Instructions to use Macro Codes

Here are the step by step instructions to use the VBA Str 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’.

Complete List of VBA Functions:

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

Leave a Comment

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