VBA STRING Function (Syntax + Example)

Last Updated: June 22, 2023
puneet-gogia-excel-champs

- Written by Puneet

The VBA STRING function is listed under the text category of VBA functions. When you use it in a VBA code, it repeats a character number of times and returns that string in the result, and if you prefer to supply more than one character it only repeats the first character out of them.

Syntax

String(Number, Character)

Arguments

  • Number: The number of times to repeat the character.
  • Character: A character code or a string that you want to be repeated.

Example

To practically understand how to use the VBA STRING function, you need to go through the below example where we have written a vba code by using it:

example-vba-string-function
Sub example_STRING()
Range("A1").Value = String(5, "Y")  
End Sub

In the above code, we have used STRING to create a string using the character “Y” and specify to repeat it 5 times and it has returned the result in cell B1.

Notes

  • If the “number” supplied is a negative number then VBA will return a run-time 5 error.
  • If the “character” is Null, it will return Null.