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 = "<br>"
myHtmlFile.Write sb.ToString()
Would print the following:
some string<br>another string<br>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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…