Join Two Strings

You can join the contents of two string variables — actually multiple strings — together to create one string. You commonly refer to the process of joining strings together as concatenation.

The only real limitation to joining strings together is the potential of exceeding the declared or maximum length of the string variable receiving the joined string. When you declare a fixed-length string variable using the Dim statement, you also typically specify that string's maximum length. A combined set of strings can contain a maximum of 65,535 characters. Variable-length string variables, on the other hand, can hold up to 2 billion characters, and you do not have to specify their length.

Each character in a string takes 1 byte of storage plus additional storage for the string header. When you declare a string, you specify the size for a fixed-length string. VBA

does not extend the size of a fixed-length string to store a larger string. If two joined strings form a string larger than the space allows, VBA truncates the string to fit the alloted space. For example, if you name a string variable Name with a fixed-length of ten characters, and you specify Name = "Hungry " & "Minds", the Name variable contains string Hungry Min. VBA truncates the remaining two characters, ds, because the string variable can only hold ten characters. Keep in mind that VBA also treats spaces that you add to the strings as characters.

Although the concatenation operator (&) joins strings together, VBA also allows you to use the + (addition) operator to combine strings. Using the concatenation operator to show a distinction between a string concatenation and an arithmetic addition statement adheres to better coding standards.


I Type a question For help no SI |[S]

VBAProject (Budget^]

B-fiS Microsoft Excel Obje | MB Sheetl (Sheetl) —® 5heet2 (5heet2) I 5heet3 (SheetS)—! ThisWorkbook B-<2j Modules luleZ Module 3

habetic | categorized | Module2



Sub Calculate

Calculate Expet an

n TestStcingl â.s Seeing * 10 (i TestString2 As String * 10 n Test5tcing3 As String * 2|o

□ Create a subroutine. 0 Press Enter.

L0 Type Dim TestString1 As String * 10, replacing TestString1 with the first string variable and 10 with the string length.


VBAPtoject (Budget

Microsoft Eïcel Obje B] Sheetl (SheetlJ

H] 5heetZ (SheetZ)

I]5heet3(Sheet3) -fi ThisWorlhoQk

. Modules ht Modulel L Modulez VBAPtoject (OfficeE:

■ You can repeat step 4 to create a second string variable.

Q Type Dim TestString3 As String * 20, replacing TestString3 with the variable to contain the concatenated string and 20 with the string length.

Module2"Module - F

Alphabetic | categorized | e)

'/ PERSONAL XLS Modulel [Code)


Sub Calculate_Exi ' Calculate Expei it

Sub Join_Strings ()

Dim TestStringl As String •

Dim TestString2 As String :

Dim Test5tring3 As String •

aS-iLj rarazr

Type TestStringl = "Excel", replacing TestString1 with the variable in step 4 and "Excel" with the string value.

■ Repeat step 6 for the second variable.

0 0

Post a comment