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

Using Drag-and-Drop to Create a Hyperlink

If you open workbooks in two instances of Excel, you can use drag-and-drop techniques to create hyperlinks from one workbook ...

Discover More

Specifying a Paper Tray in a Macro

If you are using a macro to create your printed Excel output, you may need a way to specify that paper should come from a ...

Discover More

Jumping to the End of Page after Enter

Imagine you start typing in a new document, and when you press the Enter key the cursor jumps a huge distance to the bottom ...

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)

Inserting the Current Time with Seconds

If you need to insert the current time, with seconds, then you'll need the macro discussed in this tip. It's easy to use and ...

Discover More

Displaying the Selected Cell's Address

Need to know the address of the cell that is currently selected? The function and macro highlighted in this tip will come in ...

Discover More

Displaying the "Last Modified" Date

Want to know when a workbook was last modified? Want to put that date within the header of your worksheet? Here's how 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 four less than 9?

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.