VBA String Function

VBA String Function in Excel

VBA String function in Excel is categorized as a Text/String function in VBA. It is a built-in function in MS Office Excel. Returns a string which consists of repetitive characters. It consists of 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 use 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.

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(“VBAF1.COM”)

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 returns 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(100)

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(65)

Here is a simple example of the VBA String function. This below example returns 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 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