Share via


String Operations

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

The following points provide suggestions for ways to enhance the performance of string operations:

  • Minimize concatenation operations when you can. You can use the Mid function on the left side of the equal sign to replace characters within the string, rather than concatenating them together. The drawback to using the Mid function is that the replacement string must be the same length as the substring you are replacing.

    Dim strText As String
    
    strText = "this is a test"
    Mid(strText, 11, 4) = "tent"
    Debug.Print strText
    
  • Microsoft® Visual Basic® for Applications (VBA) provides a number of intrinsic string constants that you can use to replace function calls. For example, you can use the vbCrLf constant to represent a carriage return/linefeed combination within a string, rather than using Chr(13) & Chr(10).

  • String-comparison operations are slow. Sometimes, you can avoid them by converting a character in the string to an ANSI value. For example, the following code checks whether the first character in a string is a space:

    If Asc(strText) = 32 Then
    

    The previous code is faster than the following:

    If Left(strText, 1) = " " Then
    

See Also

Optimizing VBA Code | Declaring Variables | Mathematical Operations | Loops