INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

VBA Visual Basic for Applications (Microsoft) FAQ

VBA How To

Concatenate strings more efficiently by taupirho
Posted: 10 Jun 04


If you need to concatenate serious amounts of text (i.e >100000 bytes) together and you're using the regular & or + VBA concatenation operator you'll find that performance can be increased dramatically by using the little known mid$ statement. Here's what the HELP on mid says:


Replaces a specified number of characters in a Variant (String) variable with characters from another string.

Syntax

Mid(stringvar, start[, length]) = string

The Mid statement syntax has these parts:

Part             Description
stringvar        Required. Name of string variable to    
                 modify.
start            Required; Variant (Long). Character
                 position in stringvar where the
                 replacement of text begins.
length           Optional; Variant (Long). Number of
                 characters to replace. If omitted, all of
                 string is used.
string           Required. String expression that replaces
                 part of stringvar.


Remarks

The number of characters replaced is always less than or equal to the number of characters in stringvar.

So say I have a string variable 'data_block' with 900000 characters in it and I want to concatenate another string with 100000 characters onto it - say 'data_line'. Here's how you do it with mid

' make sure our data_block variable can accommodate 1000000 characters
' and our data_line can take 100000

data_block = String$(1000000, " ")
data_line = String$(100000, " ")

' Assume we've now filled data_block with 900000 characters and data_line with 100000 characters
' We now concatenate them

Offset = len(data_block) + 1
Mid(data_block, Offset ) = data_line


Again for small strings just use the & operator but the speed increase in using mid$ for large string variables is significant.


Back to VBA Visual Basic for Applications (Microsoft) FAQ Index
Back to VBA Visual Basic for Applications (Microsoft) Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close