I am trying to learn how to use stringbuilder functions in VBA, but am having trouble finding resources in VBA on how to use them. I could use some help to use a pre-made st
String concatenation (&
) in VBA is notoriously slow, so often "stringbuilder" classes like this one are used to speed up the process if you have a large number of strings you need to combine.
The general idea is to use an Array()
to store individual string components and then combine all strings once, when you need them, using the Join()
function. The array is automatically resized as strings are added. Many use a "GrowBy"
feature (although this one doesn't) to grow the array by a static size or factor as the array limit is reached. That can improve performance as well, since calling ReDim Preserve
for every string insertion can take its toll.
To answer your question, pretend you needed to build a portion of an HTML file. You could use the shown string class like so:
Dim sb
Set sb = New StringBuilder ' Guessing here. You haven't shown the class name.
sb.Append "some string"
sb.Append "another string"
sb.Append "a third string"
....
sb.Delimiter = "
"
myHtmlFile.Write sb.ToString()
Would print the following:
some string
another string
a third string
That's the general idea. Avoid the use of &
as much as possible by using an array and you should see some significant performance improvements.