Have you ever wanted to try to swap values from one cell to another? It is possible using Macros in Excel!

Plus you also get to learn some programming along the way, how cool is that?

Make sure your Excel has the Developer Tab enabled following this tutorial.

Here are the 2 values that we want to swap:

How to Swap Values Using Macros in Excel

I will show you step by step, on how we can swap values!

I explain how you can do this below:

DOWNLOAD EXCEL WORKBOOK

STEP 1: Go to Developer > Insert > ActiveX Controls > Button

How to Swap Values Using Macros in Excel

Drag anywhere in the Excel spreadsheet to create your Button!

How to Swap Values Using Macros in Excel

 

STEP 2: Right click on your new button and select View Code.

How to Swap Values Using Macros in Excel

 

STEP 3: Type in this code:

Dim container As Double

container = Range(“A8”).Value
Range(“A8”).Value = Range(“B8”).Value
Range(“B8”).Value = container

How to Swap Values Using Macros in Excel

Let us go through each line step by step:

Dim container As Double

This creates a new variable named container wherein we can store a numerical value inside. The double type means it can have a numerical value.

container = Range(“A8”).Value

Now we are getting the first value in Cell A8 and storing it into our container variable.

Range(“A8”).Value = Range(“B8”).Value

We will now copy the content of Cell B8 and paste it into Cell A8. First step done! So far both will have the value 500 in our example.

Range(“B8”).Value = container

Now the last step! Copy the content of the container variable into Cell B8. The swap is now complete!

 

STEP 4: Time to test it out! Make sure Design Mode is deselected.

How to Swap Values Using Macros in Excel

 

STEP 5: Click on the button now and you will see the values get swapped!

How to Swap Values Using Macros in Excel

 

Swap Values Using Macros in Excel

How to Swap Values Using Macros in Excel

HELPFUL RESOURCE:

 

Free Excel Macros & VBA Webinar Training

If you like this Excel tip, please share itEmail this to someone

email

Pin on Pinterest

Share on Facebook

Tweet about this on Twitter

Share on LinkedIn

Share on Google+

Related Posts

How to Use Macro Recorder in Excel Sometimes we want to automate some steps in Excel to save us time, however it's cumbersome to code the exact macro for it - especially as a VBA beginner.But you know what?  There is an easy way around 🙂You can try out the Macro Recorder to mimic each step inside Excel an...
How to Create Your First Macro Button in Excel ... Do you repeat various Excel actions like copy & pasting data, formatting, putting borders etc?Do you know that you can automate your tasks in Excel with a couple of mouse clicks?If you keep on repeating the same thing over and over again, creating an Excel Macro is pe...
How to Add a Macro to the Quick Access Toolbar Rib... You have been following our Macro tutorials and are becoming an expert in no time!What if there's a macro that you're using all of the time, day in, day out? We can add a macro to the Quick Access Toolbar Ribbon in Excel for easy access!Make sure your Excel ribbon has the...