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

Understanding and Using Bookmarks

Bookmarks are a great feature you can use to mark the location of text or to mark a position within a document. They can be ...

Discover More

Calculating Future Workdays

Need to calculate the date that is a certain number of workdays in the future? You can do so using a couple of different ...

Discover More

WordTips Ribbon 2013 Archive (Table of Contents)

WordTips is a weekly newsletter that provides tips on how to best use Microsoft's word processing software. At the ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More ExcelTips (ribbon)

Updating Automatically when Opening Under Macro Control

If your workbook contains links, you are normally given the opportunity to update those links when you open the workbook. ...

Discover More

Replacing and Converting in a Macro

When you use a macro to process data you always run the risk of making that data unusable by Excel. This is especially true ...

Discover More

Out of Memory Errors when Accessing the VBA Editor

It can be frustrating when you get error messages doing something that you previously did with no errors. If you get an out ...

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 8Mpixels. 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 - 4?

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.