DEV Community

Andrews
Andrews

Posted on • Originally published at geekexcel.com on

SWAP Values Using Macros (VBA) in Excel Office 365 – [with Example]

SWAP Values Using Macros in Excel:

In this article, we are going to see how to Swap Values using Macros in Excel Office 365 with an example. Let’s get into this article!! Get an official version of MS Excel from the following link: https://www.microsoft.com/en-in/microsoft-365/excel

Steps to Swap Values Using Macros in Excel:

To swap two values in the Excel spreadsheet with the help of Macros, do as below instructions.

  • First, you need to enter two numbers that you want to swap.

Enter two numbers
Enter two numbers

  • On the Developer Tab, click the Insert option, then select the button icon under the ActiveX controls section.

Select the button
Select the button

  • Now, you need to drag and select the cell anywhere in the spreadsheet to insert the button.

Insert button
Insert button

  • You need to right-click on the button and select the View Code option from the menu.

Select View Code option
Select View Code option

  • You have to enter the below code and close the window.

VBA Code for SWAP Values

Dim container As Double

container = Range(“A8”).Value

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

Range(“B8”).Value = container

Enter the Code
Enter the Code

  • Now, you need to check that the Design Mode is selected or not. If is selected, you have to deselect it.

Deselect the Design Mode option
Deselect the Design Mode option

  • You have to click the created button and see that the values are swapped.

Result
Result

From the above steps, you can clearly understand how to swap two numbers using Macros.

You can find the working of above Macro in the below video.

<!--[if lt IE 9]>document.createElement(&#39;video&#39;);<![endif]-->

Wind-Up:

In this article, you can learn the steps to Swap Values using Macros in Excel Office 365 with an example. Drop your feedback in the comment section. Thanks for visiting Geek Excel. Keep Learning!!

Top comments (0)