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

Hiding Gridlines

For those times when you remove the borders from your tables, Word provides a way that you can display non-printing ...

Discover More

Finding and Changing Word's Internal Commands

If you know how to create macros, you can easily create entire replacements for Word's internal commands. Here's all you ...

Discover More

Defining a Custom Paper Size

Need to print your worksheet on a non-standard paper size? Excel is rather limited in printing to such papers, and here ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (ribbon)

Using Named Ranges in a Macro

Named ranges are a great capability provided by Excel. You can define all sorts of named ranges in a workbook, but how do ...

Discover More

Adding Buttons to Your Worksheet

You can easily add a button to your worksheet that will allow you to run various macros. This tip shows how easy it is.

Discover More

Forcing Manual Calculation For a Workbook

If you have a large, complex workbook, you may want to make sure that it is always calculated manually instead of ...

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 minus 0?

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.