Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, and 2013. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Swapping Two Strings.

Swapping Two Strings

by Allen Wyatt
(last updated November 22, 2014)

2

If you do any serious macro programming, there will eventually come a time when you want to swap the values in two strings. In some versions of BASIC, there are commands that handle this, but there isn't in VBA. The following very simple technique should do the trick for most people:

TempString = MyString1
MyString1 = MyString2
MyString2 = TempString

When completed, the values in MyString1 and MyString2 have been swapped, and TempString doesn't matter, since it was intended (by this technique) as a temporary variable anyway.

If you prefer to not use a temporary variable (for whatever reason), you could use the following code to swap two strings:

MyString1 = MyString1 & MyString2
MyString2 = Left(MyString1, Len(MyString1) - Len(MyString2))
MyString1 = Right(MyString1, Len(MyString1) - Len(MyString2))

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (10207) applies to Microsoft Excel 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Excel here: Swapping Two Strings.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

A Fast Find-Next

Want a quick, easy way to "search again" for the next occurrence of what you need? Use the technique in this tip and ...

Discover More

Missing Fonts in a Letterhead

When you create a document (such as a letterhead) that you want multiple people to use, you need to be concerned with ...

Discover More

Default Formatting for PivotTables

Wish there was a way to define how you want PivotTables formatted before you actually create the PivotTable? You may be ...

Discover More

Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!

More ExcelTips (ribbon)

Working with Colors in a Macro

The colors used in a Excel are not as simple as they used to be. Here are some ideas relative to working with those ...

Discover More

Converting Phone Numbers

Sometimes you receive a phone number that contains alphabetic characters and you need to convert it to a purely numeric ...

Discover More

Default Worksheet when Opening

When opening a workbook, you may want to make sure that a particular worksheet is always displayed first. The only way to ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is one minus 1?

2016-01-10 01:59:51

Rick Rothstein

Similar to Micky's method for eliminating the need for a temporary variable, but using the Replace function instead of the Split function...

Sub Swap_2_Variables1_Without_TempVar()
Dim Var1 As String, Var2 As String
Var1 = "ABC": Var2 = "CBA"
Var1 = Var1 & Var2
Var2 = Replace(Var1, Var2, "")
Var1 = Replace(Var1, Var2, "")
MsgBox "Variable Var1 was swapped to: " & Var1
MsgBox "Variable Var2 was swapped to: " & Var2
End Sub


2014-11-22 13:34:23

Michael (Micky) Avidan

No need for a temporary variable.
Try:
Sub Swap_2_Variables1_Without_TempVar()
Var1 = "ABC": Var2 = "CBA"
Var1 = Var1 & "," & Var2
Var2 = Split(Var1, ",")(0)
Var1 = Split(Var1, ",")(1)
MsgBox "Variable Var1 was swapped to: " & Var1
MsgBox "Variable Var2 was swapped to: " & Var2
End Sub


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.