Excel VBA String Function

The String function in VBA creates a String, consisting of several repeated characters.

Syntax

String (Number, Character)

Parameter

Number (required) – This parameter represents the number of characters in the returned String.

Character (required) – This parameter signifies a character code or a String, representing the character that is to be repeated in the returned String.

Return

This parameter returns a String consisting of several characters. If the Number or Character arguments are Null, the String function returns Null.

Example 1

Sub StringFunction_Example1()
 Dim str1 As String
 Dim str2 As String
 Dim str3 As String
 str1 = String(10, 97)
 Cells(1, 1) = str1
 str2 = String(10, "a") 
 Cells(1, 2) = str2
 str3 = String(10, "about")
 Cells(1, 3) = str3
 ' All the three variables will return "aaaaaaaaaa".
 End Sub 

Output

aaaaaaaaaa aaaaaaaaaa aaaaaaaaaa
VBA String Function