How to use the VBA STRING Function (Syntax + Example)

HomeVBA Functions LIST (Category Wise)How to use the VBA STRING Function (Syntax + Example)

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 VBA STRING function, you need to go through the below example where we have written a vba code by using it:

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 the 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.

Related Functions

FORMAT | INSTR | INSTRREV | LCASE | LEFT | LEN | LTRIM | MID | REPLACE | RIGHT | RTRIM | SPACE | STRCOMP | STRCONV | STRREVERSE | TRIM | UCASE