VBA String Function

VBA String Function

Overview of VBA String Function:

VBA String function is categorized as a Text/String function in VBA. It is a built-in function in MS Office Excel. It returns a string which consists of repeated characters. It has two required parameters. If number or character arguments are null, it returns Null. This function could be used as a VBA function and can’t be used as a Excel Worksheet function.

The String function can be used in either procedure or function in a VBA editor window in Excel. We can use this VBA String 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 String function, where we can use this String function and real-time examples in VBA.

Table of Contents:

Syntax of VBA String Function

The syntax of the VBA String function is

String(Number, Character)

Note: This String function returns a String with a specified character the specified number of times.

Parameters or Arguments

This function has two mandatory parameters for the String Function.
Where
Number: The Number is a mandatory argument. It represents the number of characters in the result.
Character: The Character is a mandatory string argument. It represents a character, Which has to be repeated for specified number of times in the result. If a string contains more than one character, it considers only the first character of the String.

Where we can apply or use the VBA String Function?

We can use this VBA String 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: Example on VBA String Function

Here is a simple example of the VBA String function. This below example eturns a string which consists of repeated characters. The output of below macro is ‘VVV’

'Example on VBA String Function
Sub VBA_String_Function_Ex1()

    Dim sString As String
    Dim sOutput As String
    
    sString = "VBAF1.COM"
    
    sOutput = String(3, sString)
    
    MsgBox "Result :" & sOutput, vbInformation, "VBA String Function"

End Sub

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

Example 2: Example on VBA String Function

Here is a simple example of the VBA String function. This below example eturns a string which consists of repeated characters. The output of below macro is ‘@@@@’

'Example on VBA String Function
Sub VBA_String_Function_Ex2()

    Dim sString As String
    Dim sOutput As String
    
    sString = "@"
    
    sOutput = String(4, sString)
    
    MsgBox "Output :" & sOutput, vbInformation, "VBA String Function"

End Sub

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

VBA String Function

Example 3: Example on VBA String Function

Here is a simple example of the VBA String function. This below example checks the specified expression(CVErr(8)) is an error or not. The output of below macro is ‘ddddd’ TRUE.

'Example on VBA String Function
Sub VBA_String_Function_Ex3()

    Dim iInput As Integer
    Dim sOutput As String
    
    sString = 100
    
    sOutput = String(5, sString)
    
    MsgBox "Result :" & sOutput, vbInformation, "VBA String Function"

End Sub

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

Example 4: Example on VBA String Function

Here is a simple example of the VBA String function. This below example eturns a string which consists of repeated characters. The output of below macro is ‘666’.

'Example on VBA String Function
Sub VBA_String_Function_Ex4()

    Dim sString As String
    Dim sOutput As String
    
    sString = 65
    
    sOutput = String(3, sString)
    
    MsgBox "Output :" & sOutput, vbInformation, "VBA String Function"

End Sub

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

Instructions to use Macro Codes

Here are the step by step instructions to use the VBA String 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 *