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))

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

End-of-Month Calculations

Don't want to use the EOMONTH function to figure out the end of a given month? Here are some other ideas for discovering ...

Discover More

Sorting with Graphics

If the graphics that you insert in your worksheet meet a couple of simple requirements, it is possible to have those ...

Discover More

Using AutoFiltering

Excel's AutoFilter tool is a great way to make a long list of items much more manageable. This tip explains how to set up ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (ribbon)

Setting Row Height in a Macro

Macros can be used to change the formatting of your worksheet, if desired. One change you might want to make is to the ...

Discover More

Understanding the Select Case Structure

One of the powerful programming structures available in VBA is the Select Case structure. This tip explains how you can ...

Discover More

Using a Macro to Select a Modified Table Body

One of the big challenges when processing your data using macros is to make sure that you select all the data (and just ...

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 6 - 3?

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.