VBA STR Function

VBA STR Function in Excel

VBA Str function in Excel 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.

use 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.

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: Let us 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 Run VBA Macro Code or Procedure:

You can refer the following link for the step by step instructions.

Instructions to run VBA Macro Code

Other Useful Resources:

Click on the following links of the useful resources. These helps to learn and gain more knowledge.

VBA Tutorial VBA Functions List VBA Arrays in Excel Blog

VBA Editor Keyboard Shortcut Keys List VBA Interview Questions & Answers

Leave a Reply