The VBA String function creates a String, consisting of a number of repeated characters.
The syntax of the function is:
Where the function arguments are:
Number | - |
The number of characters in the returned String. |
Character | - |
A character code or a String, representing the character that is to be repeated in the returned String. Note:
|
If the Number or Character arguments are Null, the String function returns Null.
The following VBA code uses the String function to create the String "aaaaaaaaaa" using three different Character arguments.
' Use the String function to create the String "aaaaaaaaaa".
Dim str1 As String
Dim str2 As String Dim str3 As String
str1 = String( 10, 97 )
' The variables str1, str2 and str3 are now all equal to "aaaaaaaaaa".
str2 = String( 10, "a" ) str3 = String( 10, "alpha" ) |
In the above example, the three calls to the String function all return the String "aaaaaaaaaa".
Note that:
In the first call to the String function, the supplied Character argument is the character code 97, which is the character code for the character "a".
Therefore the String function returns the character "a" repeated 10 times.In the third call to the String function, the supplied Character argument is the String "alpha".
The String function only uses the first character of a supplied String and so, in this case also, the function returns the character "a" repeated 10 times.If you supply a negative Number argument to the VBA String function, you will get the error: