Are VBA strings immutable?

excel, string, vba

Solution

While VB.NET strings are immutable, as mandated by System.String, VBA (including VB6?) strings can be mutated (such as with `Mid$`). See Alex K's answer and note the `StrPtr` result after the operations.

Original answer; supported by documentation, in opposition to a counter-example.

VBA strings are immutable.

Just as with VB.NET, there is no way to "replace part of" or "append to" a string without creating a new string. Whether or not this matters - as modern computers are pretty darn fast - depends on the actual algorithm, data, and environment.

Unlike .NET documentation, such a behavior reference for VBA is [becoming] difficult to track down. From MS-VBAL: 2.1 Data Values and Value Types, we find this rare little gem

Individual data values are immutable. This means that there are no defined mechanisms available within a VBA Environment that can cause a data value to change into another data value.

where Strings represent "individual data values".

Problem

I know .NET (and therefore VB.NET) strings are immutable. However, I'm using VBA 7.0 in Excel 2010. Are strings there immutable? I'm doing a lot of string processing and for small quantities, some (direct) string manipulation is fine, but I'm worried it won't scale - since every additional character moved from one string to another might create yet another instance of the string.

Original source