{"id":8883,"date":"2018-08-03T10:20:41","date_gmt":"2018-08-03T08:20:41","guid":{"rendered":"https:\/\/www.myexcelonline.com\/?p=8883"},"modified":"2024-03-22T22:04:06","modified_gmt":"2024-03-22T21:04:06","slug":"how-to-swap-values-using-macros-in-excel","status":"publish","type":"post","link":"https:\/\/www.myexcelonline.com\/meo-staging\/blog\/how-to-swap-values-using-macros-in-excel\/","title":{"rendered":"How to Swap Values Using Macros in Excel"},"content":{"rendered":"<p>Have you ever wanted to try to <a href=\"https:\/\/www.myexcelonline.com\/meo-staging\/blog\/swap-columns-in-excel\/\" target=\"_blank\" rel=\"noopener\">swap values from one cell<\/a> to another? It is possible using <a class=\"wpil_keyword_link\" href=\"https:\/\/www.myexcelonline.com\/meo-staging\/blog\/101-excel-macros-examples\/\" target=\"_blank\" rel=\"noopener\" title=\"Macros\" data-wpil-keyword-link=\"linked\">Macros<\/a> in Excel!<\/p>\n<p>Plus you also get to learn some programming along the way, how cool is that?<\/p>\n<p>Make sure your Excel has the Developer Tab enabled following\u00a0<a href=\"https:\/\/www.myexcelonline.com\/meo-staging\/blog\/how-to-create-your-first-macro-button-in-excel-beginners-tutorial\/\" target=\"_blank\" rel=\"noopener noreferrer\">this tutorial<\/a>.<\/p>\n<p>Here are the 2 values that we want to swap:<\/p>\n<p><a href=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2018\/05\/How-to-Swap-Values-Using-Macros-in-Excel-00.jpg\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" title=\"How to Swap Values Using Macros in Excel\" class=\"alignnone size-full wp-image-8886\" src=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2018\/05\/How-to-Swap-Values-Using-Macros-in-Excel-00.jpg\" alt=\"How to Swap Values Using Macros in Excel\" width=\"759\" height=\"253\" srcset=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2018\/05\/How-to-Swap-Values-Using-Macros-in-Excel-00.jpg 759w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2018\/05\/How-to-Swap-Values-Using-Macros-in-Excel-00-300x100.jpg 300w\" sizes=\"(max-width: 759px) 100vw, 759px\" \/><\/a><\/p>\n<p>I will show you step by step, on how we can swap values!<\/p>\n<p>I explain how you can do this below:<\/p>\n<div class=\"after-post-box\">\n<p><iframe src=\"https:\/\/www.youtube.com\/embed\/CrHAxaCarXk?rel=0 \" width=\"800\" height=\"400\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><span data-mce-type=\"bookmark\" style=\"width: 0px;overflow: hidden;line-height: 0\" class=\"mce_SELRES_start\">\ufeff<\/span><\/iframe><a href=\"https:\/\/www.youtube.com\/c\/MyExcelOnlinecomtutorials?sub_confirmation=1\" target=\"_blank\" rel=\"noopener noreferrer nofollow\"><img loading=\"lazy\" decoding=\"async\" class=\"alignmiddle wp-image-111468 size-full\" style=\"vertical-align: middle\" src=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2019\/04\/1.yt_.png\" alt=\"\" title=\"\"><\/a><\/p>\n<p class=\"line-two\"><a class=\"IRPP_minimalist icon-download button small\" href=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2018\/05\/How-to-Swap-Values-Using-Macros-in-Excel.xlsm\" target=\"_blank\" rel=\"noopener noreferrer\" data-leadbox=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2018\/05\/How-to-Swap-Values-Using-Macros-in-Excel.xlsm\" data-leadbox-id=\"147284833f72a2:12f08263ab46dc\"><span class=\"ctaText px-3 text-capitalize\">Download excel workbook<i class=\"fa fa-download\"><\/i><\/span><span class=\"postTitle px-3\">How-to-Swap-Values-Using-Macros-in-Excel.xlsm<\/span><\/a><\/p>\n<p class=\"line-two\">\n<\/div>\n<p><span style=\"color: #ff0000\"><strong>STEP 1:<\/strong><\/span>\u00a0Go to\u00a0<em><strong>Developer &gt; Insert &gt; ActiveX Controls &gt; Button<\/strong><\/em><\/p>\n<p><a href=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2018\/05\/How-to-Swap-Values-Using-Macros-in-Excel-01.jpg\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" title=\"How to Swap Values Using Macros in Excel\" class=\"alignnone size-full wp-image-8887\" src=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2018\/05\/How-to-Swap-Values-Using-Macros-in-Excel-01.jpg\" alt=\"How to Swap Values Using Macros in Excel\" width=\"295\" height=\"263\" \/><\/a><\/p>\n<p>Drag anywhere in the Excel <a class=\"wpil_keyword_link\" href=\"https:\/\/www.myexcelonline.com\/meo-staging\/blog\/free-excel-templates-and-spreadsheets\/\" target=\"_blank\" rel=\"noopener\" title=\"spreadsheet\" data-wpil-keyword-link=\"linked\">spreadsheet<\/a> to create your Button!<\/p>\n<p><a href=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2018\/05\/How-to-Swap-Values-Using-Macros-in-Excel-02.jpg\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" title=\"How to Swap Values Using Macros in Excel\" class=\"alignnone size-full wp-image-8888\" src=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2018\/05\/How-to-Swap-Values-Using-Macros-in-Excel-02.jpg\" alt=\"How to Swap Values Using Macros in Excel\" width=\"759\" height=\"253\" srcset=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2018\/05\/How-to-Swap-Values-Using-Macros-in-Excel-02.jpg 759w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2018\/05\/How-to-Swap-Values-Using-Macros-in-Excel-02-300x100.jpg 300w\" sizes=\"(max-width: 759px) 100vw, 759px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000\"><strong>STEP 2:<\/strong><\/span> Right click on your new button and select\u00a0<strong>View Code<\/strong>.<\/p>\n<p><a href=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2018\/05\/How-to-Swap-Values-Using-Macros-in-Excel-03.jpg\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" title=\"How to Swap Values Using Macros in Excel\" class=\"alignnone size-full wp-image-8889\" src=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2018\/05\/How-to-Swap-Values-Using-Macros-in-Excel-03.jpg\" alt=\"How to Swap Values Using Macros in Excel\" width=\"348\" height=\"285\" srcset=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2018\/05\/How-to-Swap-Values-Using-Macros-in-Excel-03.jpg 348w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2018\/05\/How-to-Swap-Values-Using-Macros-in-Excel-03-300x246.jpg 300w\" sizes=\"(max-width: 348px) 100vw, 348px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000\"><strong>STEP 3:\u00a0<\/strong><\/span>Type in this code:<\/p>\n<p><em>Dim container As Double<\/em><\/p>\n<p><em>container = Range(&#8220;A8&#8221;).Value<\/em><br \/>\n<em>Range(&#8220;A8&#8221;).Value = Range(&#8220;B8&#8221;).Value<\/em><br \/>\n<em>Range(&#8220;B8&#8221;).Value = container<\/em><\/p>\n<p><a href=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2018\/05\/How-to-Swap-Values-Using-Macros-in-Excel-04.jpg\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" title=\"How to Swap Values Using Macros in Excel\" class=\"alignnone size-full wp-image-8890\" src=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2018\/05\/How-to-Swap-Values-Using-Macros-in-Excel-04.jpg\" alt=\"How to Swap Values Using Macros in Excel\" width=\"411\" height=\"217\" srcset=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2018\/05\/How-to-Swap-Values-Using-Macros-in-Excel-04.jpg 411w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2018\/05\/How-to-Swap-Values-Using-Macros-in-Excel-04-300x158.jpg 300w\" sizes=\"(max-width: 411px) 100vw, 411px\" \/><\/a><\/p>\n<p>Let us go through each line step by step:<\/p>\n<p><strong><em>Dim container As Double<\/em><\/strong><\/p>\n<p>This creates a new variable named\u00a0<strong>container<\/strong> wherein we can store a numerical value inside.\u00a0The\u00a0<strong>double\u00a0<\/strong>type means it can have a numerical value.<\/p>\n<p><strong><em>container = Range(&#8220;A8&#8221;).Value<\/em><\/strong><\/p>\n<p>Now we are getting the first value in Cell A8 and storing it into our\u00a0<strong>container\u00a0<\/strong>variable.<\/p>\n<p><strong><em>Range(&#8220;A8&#8221;).Value = Range(&#8220;B8&#8221;).Value<\/em><\/strong><\/p>\n<p>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\u00a0<strong>500<\/strong> in our example.<\/p>\n<p><strong><em>Range(&#8220;B8&#8221;).Value = container<\/em><\/strong><\/p>\n<p>Now the last step! Copy the content of the\u00a0<strong>container\u00a0<\/strong>variable into Cell B8. The swap is now complete!<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000\"><strong>STEP 4:<\/strong><\/span>\u00a0Time to test it out! Make sure\u00a0<strong>Design Mode is deselected.<\/strong><\/p>\n<p><a href=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2018\/05\/How-to-Swap-Values-Using-Macros-in-Excel-05.jpg\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" title=\"How to Swap Values Using Macros in Excel\" class=\"alignnone size-full wp-image-8891\" src=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2018\/05\/How-to-Swap-Values-Using-Macros-in-Excel-05.jpg\" alt=\"How to Swap Values Using Macros in Excel\" width=\"231\" height=\"131\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000\"><strong>STEP 5:<\/strong><\/span>\u00a0Click on the button now and you will see the values get swapped!<\/p>\n<p><a href=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2018\/05\/How-to-Swap-Values-Using-Macros-in-Excel-06.jpg\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" title=\"How to Swap Values Using Macros in Excel\" class=\"alignnone size-full wp-image-8892\" src=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2018\/05\/How-to-Swap-Values-Using-Macros-in-Excel-06.jpg\" alt=\"How to Swap Values Using Macros in Excel\" width=\"609\" height=\"94\" srcset=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2018\/05\/How-to-Swap-Values-Using-Macros-in-Excel-06.jpg 609w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2018\/05\/How-to-Swap-Values-Using-Macros-in-Excel-06-300x46.jpg 300w\" sizes=\"(max-width: 609px) 100vw, 609px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #0000ff\"><strong>Swap Values Using Macros in Excel<\/strong><\/span><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/www.myexcelonline.com\/meo-staging\/100-66.html\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13266\" src=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2019\/10\/101-Macros-Book.png\" alt=\"101 Macros Book\" width=\"1200\" height=\"628\" srcset=\"https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2019\/10\/101-Macros-Book.png 1200w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2019\/10\/101-Macros-Book-300x157.png 300w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2019\/10\/101-Macros-Book-1024x536.png 1024w, https:\/\/www.myexcelonline.com\/meo-staging\/wp-content\/uploads\/2019\/10\/101-Macros-Book-768x402.png 768w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" title=\"\"><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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\u00a0this tutorial. Here are the 2 values that we want [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":17613,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"cos_headline_score":0,"cos_seo_score":0,"cos_headline_text":"","cos_headline_has_been_analyzed":false,"cos_last_analyzed_headline":[],"om_disable_all_campaigns":false},"categories":[56,639],"tags":[371,372],"acf":[],"_links":{"self":[{"href":"https:\/\/www.myexcelonline.com\/meo-staging\/wp-json\/wp\/v2\/posts\/8883"}],"collection":[{"href":"https:\/\/www.myexcelonline.com\/meo-staging\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.myexcelonline.com\/meo-staging\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.myexcelonline.com\/meo-staging\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.myexcelonline.com\/meo-staging\/wp-json\/wp\/v2\/comments?post=8883"}],"version-history":[{"count":0,"href":"https:\/\/www.myexcelonline.com\/meo-staging\/wp-json\/wp\/v2\/posts\/8883\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.myexcelonline.com\/meo-staging\/wp-json\/wp\/v2\/media\/17613"}],"wp:attachment":[{"href":"https:\/\/www.myexcelonline.com\/meo-staging\/wp-json\/wp\/v2\/media?parent=8883"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.myexcelonline.com\/meo-staging\/wp-json\/wp\/v2\/categories?post=8883"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.myexcelonline.com\/meo-staging\/wp-json\/wp\/v2\/tags?post=8883"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}