{"id":387,"date":"2019-01-22T21:04:35","date_gmt":"2019-01-22T21:04:35","guid":{"rendered":"http:\/\/www.vbastring.com\/blog\/?p=387"},"modified":"2019-02-09T03:57:48","modified_gmt":"2019-02-09T03:57:48","slug":"how-to-run-a-macro-when-cell-value-changes-in-excel-vba","status":"publish","type":"post","link":"https:\/\/vbastring.com\/blog\/2019\/01\/22\/how-to-run-a-macro-when-cell-value-changes-in-excel-vba\/","title":{"rendered":"How To Run A Macro When Cell Value Changes In Excel VBA"},"content":{"rendered":"<p>Sometimes you want to be able to monitor a range of cells in your worksheet for any changes.  Using this, you can <strong>run a macro when a cell changes to a specific value<\/strong>.<\/p>\n<p>Here I will show <strong>how to run a macro when a cell value changes in Excel VBA<\/strong><\/p>\n<p>This post will also answer the question you may have, &#8220;How do I automatically trigger a macro in Excel?&#8221;  When the cell value changes, we will trigger a macro to run.<\/p>\n<p>In the following example, I am monitoring the range &#8220;A1:A4&#8221; (blue shading) for changes. <\/p>\n<p>I want the user to be made aware when the value of the cell changes and <strong>what the previous value of the cell was<\/strong>.  That way I can write code to have <strong>excel vba undo the cell change<\/strong> .<\/p>\n<p> <a href=\"https:\/\/i0.wp.com\/www.vbastring.com\/blog\/wp-content\/uploads\/2019\/01\/Run_A_Macro_When_Cell_Value_Changes_In_Excel_VBA.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/www.vbastring.com\/blog\/wp-content\/uploads\/2019\/01\/Run_A_Macro_When_Cell_Value_Changes_In_Excel_VBA.png?resize=601%2C476\" alt=\"\" width=\"601\" height=\"476\" class=\"alignleft size-full wp-image-392\" \/><\/a><\/p>\n<h2>Here&#8217;s the code<\/h2>\n<pre class=\"lang:vb decode:true \" >Dim m_dblOldValue As Double\r\n\r\n\r\nPrivate Sub Worksheet_Change(ByVal Target As Range)\r\n    \r\n    Dim rngMonitor As Range\r\n    Dim rngCell As Range\r\n    \r\n    'monitor this range of cells\r\n    Set rngMonitor = Range(\"A1:A4\")\r\n    \r\n    '*********************************************\r\n    'was one of the values in the range changed?\r\n    '*********************************************\r\n    \r\n    'loop through each cell in the range...\r\n    For Each rngCell In rngMonitor\r\n        'if the current cell address matches one of the cells to monitor, flag it with a message\r\n        \r\n        If Target.Address = rngCell.Address Then\r\n           'I get the previous value of the cell from the \"SelectionChange\" event\r\n            MsgBox Target.Address &amp; \" changed from \" &amp; m_dblOldValue &amp; \" to \" &amp; Target.Value\r\n        End If\r\n    Next\r\n    \r\nEnd Sub\r\n\r\nPrivate Sub Worksheet_SelectionChange(ByVal Target As Range)\r\n    'Get the previous value of the changed cell\r\n    m_dblOldValue = Target.Value\r\nEnd Sub\r\n<\/pre>\n<p>In the &#8220;Worksheet_SelectionChange&#8221; event, I can get the previous value.<br \/>\nIn the &#8220;Worksheet_WorksheetChange&#8221; event, I can monitor the cell changes.<\/p>\n<p>Watch me do it:<\/p>\n<p><iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/Z9kt86I_rq0\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/p>\n<p><a href=\"http:\/\/www.getyourboomback.com\/#_l_2pn\"><img data-recalc-dims=\"1\" height=\"580\" width=\"580\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/leaddyno-client-images.s3.amazonaws.com\/cffa678c5db61bb2476a50e2198c69454765190c\/6bc94a38828c6e5e788052b1673c62aecb0d487b_aminoboosters-YTE-square-2bottles-580x580.png?resize=580%2C580&#038;ssl=1\" \/><\/a><\/p>\n<p>Let me know if you have any questions.<\/p>\n<p>Use this form to send me a message:<\/p>\n<p>[simple_contact_form]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes you want to be able to monitor a range of cells in your worksheet for any changes. Using this, you can run a macro when a cell changes to a specific value. Here I will show how to run a macro when a cell value changes in Excel VBA This post will also answer [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","om_disable_all_campaigns":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-387","post","type-post","status-publish","format-standard","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How To Run A Macro When Cell Value Changes In Excel VBA - My Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/vbastring.com\/blog\/2019\/01\/22\/how-to-run-a-macro-when-cell-value-changes-in-excel-vba\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How To Run A Macro When Cell Value Changes In Excel VBA - My Blog\" \/>\n<meta property=\"og:description\" content=\"Sometimes you want to be able to monitor a range of cells in your worksheet for any changes. Using this, you can run a macro when a cell changes to a specific value. Here I will show how to run a macro when a cell value changes in Excel VBA This post will also answer [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/vbastring.com\/blog\/2019\/01\/22\/how-to-run-a-macro-when-cell-value-changes-in-excel-vba\/\" \/>\n<meta property=\"og:site_name\" content=\"My Blog\" \/>\n<meta property=\"article:published_time\" content=\"2019-01-22T21:04:35+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-02-09T03:57:48+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.vbastring.com\/blog\/wp-content\/uploads\/2019\/01\/Run_A_Macro_When_Cell_Value_Changes_In_Excel_VBA.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2019\\\/01\\\/22\\\/how-to-run-a-macro-when-cell-value-changes-in-excel-vba\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2019\\\/01\\\/22\\\/how-to-run-a-macro-when-cell-value-changes-in-excel-vba\\\/\"},\"author\":{\"name\":\"admin\",\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/#\\\/schema\\\/person\\\/e1de0b30e98940381697872449c341f5\"},\"headline\":\"How To Run A Macro When Cell Value Changes In Excel VBA\",\"datePublished\":\"2019-01-22T21:04:35+00:00\",\"dateModified\":\"2019-02-09T03:57:48+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2019\\\/01\\\/22\\\/how-to-run-a-macro-when-cell-value-changes-in-excel-vba\\\/\"},\"wordCount\":195,\"image\":{\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2019\\\/01\\\/22\\\/how-to-run-a-macro-when-cell-value-changes-in-excel-vba\\\/#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/www.vbastring.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/01\\\/Run_A_Macro_When_Cell_Value_Changes_In_Excel_VBA.png\",\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2019\\\/01\\\/22\\\/how-to-run-a-macro-when-cell-value-changes-in-excel-vba\\\/\",\"url\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2019\\\/01\\\/22\\\/how-to-run-a-macro-when-cell-value-changes-in-excel-vba\\\/\",\"name\":\"How To Run A Macro When Cell Value Changes In Excel VBA - My Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2019\\\/01\\\/22\\\/how-to-run-a-macro-when-cell-value-changes-in-excel-vba\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2019\\\/01\\\/22\\\/how-to-run-a-macro-when-cell-value-changes-in-excel-vba\\\/#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/www.vbastring.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/01\\\/Run_A_Macro_When_Cell_Value_Changes_In_Excel_VBA.png\",\"datePublished\":\"2019-01-22T21:04:35+00:00\",\"dateModified\":\"2019-02-09T03:57:48+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/#\\\/schema\\\/person\\\/e1de0b30e98940381697872449c341f5\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2019\\\/01\\\/22\\\/how-to-run-a-macro-when-cell-value-changes-in-excel-vba\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/vbastring.com\\\/blog\\\/2019\\\/01\\\/22\\\/how-to-run-a-macro-when-cell-value-changes-in-excel-vba\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2019\\\/01\\\/22\\\/how-to-run-a-macro-when-cell-value-changes-in-excel-vba\\\/#primaryimage\",\"url\":\"http:\\\/\\\/www.vbastring.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/01\\\/Run_A_Macro_When_Cell_Value_Changes_In_Excel_VBA.png\",\"contentUrl\":\"http:\\\/\\\/www.vbastring.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/01\\\/Run_A_Macro_When_Cell_Value_Changes_In_Excel_VBA.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2019\\\/01\\\/22\\\/how-to-run-a-macro-when-cell-value-changes-in-excel-vba\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How To Run A Macro When Cell Value Changes In Excel VBA\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/\",\"name\":\"My Blog\",\"description\":\"My WordPress Blog\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/#\\\/schema\\\/person\\\/e1de0b30e98940381697872449c341f5\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b2feaa698a4bd91b409df1beb5ff6acc2d7842d4f78543d413ebd468bc0171af?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b2feaa698a4bd91b409df1beb5ff6acc2d7842d4f78543d413ebd468bc0171af?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b2feaa698a4bd91b409df1beb5ff6acc2d7842d4f78543d413ebd468bc0171af?s=96&d=mm&r=g\",\"caption\":\"admin\"},\"sameAs\":[\"https:\\\/\\\/vbastring.com\\\/blog\"],\"url\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/author\\\/admin\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How To Run A Macro When Cell Value Changes In Excel VBA - My Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/vbastring.com\/blog\/2019\/01\/22\/how-to-run-a-macro-when-cell-value-changes-in-excel-vba\/","og_locale":"en_US","og_type":"article","og_title":"How To Run A Macro When Cell Value Changes In Excel VBA - My Blog","og_description":"Sometimes you want to be able to monitor a range of cells in your worksheet for any changes. Using this, you can run a macro when a cell changes to a specific value. Here I will show how to run a macro when a cell value changes in Excel VBA This post will also answer [&hellip;]","og_url":"https:\/\/vbastring.com\/blog\/2019\/01\/22\/how-to-run-a-macro-when-cell-value-changes-in-excel-vba\/","og_site_name":"My Blog","article_published_time":"2019-01-22T21:04:35+00:00","article_modified_time":"2019-02-09T03:57:48+00:00","og_image":[{"url":"http:\/\/www.vbastring.com\/blog\/wp-content\/uploads\/2019\/01\/Run_A_Macro_When_Cell_Value_Changes_In_Excel_VBA.png","type":"","width":"","height":""}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/vbastring.com\/blog\/2019\/01\/22\/how-to-run-a-macro-when-cell-value-changes-in-excel-vba\/#article","isPartOf":{"@id":"https:\/\/vbastring.com\/blog\/2019\/01\/22\/how-to-run-a-macro-when-cell-value-changes-in-excel-vba\/"},"author":{"name":"admin","@id":"https:\/\/vbastring.com\/blog\/#\/schema\/person\/e1de0b30e98940381697872449c341f5"},"headline":"How To Run A Macro When Cell Value Changes In Excel VBA","datePublished":"2019-01-22T21:04:35+00:00","dateModified":"2019-02-09T03:57:48+00:00","mainEntityOfPage":{"@id":"https:\/\/vbastring.com\/blog\/2019\/01\/22\/how-to-run-a-macro-when-cell-value-changes-in-excel-vba\/"},"wordCount":195,"image":{"@id":"https:\/\/vbastring.com\/blog\/2019\/01\/22\/how-to-run-a-macro-when-cell-value-changes-in-excel-vba\/#primaryimage"},"thumbnailUrl":"http:\/\/www.vbastring.com\/blog\/wp-content\/uploads\/2019\/01\/Run_A_Macro_When_Cell_Value_Changes_In_Excel_VBA.png","inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/vbastring.com\/blog\/2019\/01\/22\/how-to-run-a-macro-when-cell-value-changes-in-excel-vba\/","url":"https:\/\/vbastring.com\/blog\/2019\/01\/22\/how-to-run-a-macro-when-cell-value-changes-in-excel-vba\/","name":"How To Run A Macro When Cell Value Changes In Excel VBA - My Blog","isPartOf":{"@id":"https:\/\/vbastring.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/vbastring.com\/blog\/2019\/01\/22\/how-to-run-a-macro-when-cell-value-changes-in-excel-vba\/#primaryimage"},"image":{"@id":"https:\/\/vbastring.com\/blog\/2019\/01\/22\/how-to-run-a-macro-when-cell-value-changes-in-excel-vba\/#primaryimage"},"thumbnailUrl":"http:\/\/www.vbastring.com\/blog\/wp-content\/uploads\/2019\/01\/Run_A_Macro_When_Cell_Value_Changes_In_Excel_VBA.png","datePublished":"2019-01-22T21:04:35+00:00","dateModified":"2019-02-09T03:57:48+00:00","author":{"@id":"https:\/\/vbastring.com\/blog\/#\/schema\/person\/e1de0b30e98940381697872449c341f5"},"breadcrumb":{"@id":"https:\/\/vbastring.com\/blog\/2019\/01\/22\/how-to-run-a-macro-when-cell-value-changes-in-excel-vba\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/vbastring.com\/blog\/2019\/01\/22\/how-to-run-a-macro-when-cell-value-changes-in-excel-vba\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/vbastring.com\/blog\/2019\/01\/22\/how-to-run-a-macro-when-cell-value-changes-in-excel-vba\/#primaryimage","url":"http:\/\/www.vbastring.com\/blog\/wp-content\/uploads\/2019\/01\/Run_A_Macro_When_Cell_Value_Changes_In_Excel_VBA.png","contentUrl":"http:\/\/www.vbastring.com\/blog\/wp-content\/uploads\/2019\/01\/Run_A_Macro_When_Cell_Value_Changes_In_Excel_VBA.png"},{"@type":"BreadcrumbList","@id":"https:\/\/vbastring.com\/blog\/2019\/01\/22\/how-to-run-a-macro-when-cell-value-changes-in-excel-vba\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/vbastring.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How To Run A Macro When Cell Value Changes In Excel VBA"}]},{"@type":"WebSite","@id":"https:\/\/vbastring.com\/blog\/#website","url":"https:\/\/vbastring.com\/blog\/","name":"My Blog","description":"My WordPress Blog","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/vbastring.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/vbastring.com\/blog\/#\/schema\/person\/e1de0b30e98940381697872449c341f5","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/b2feaa698a4bd91b409df1beb5ff6acc2d7842d4f78543d413ebd468bc0171af?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/b2feaa698a4bd91b409df1beb5ff6acc2d7842d4f78543d413ebd468bc0171af?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b2feaa698a4bd91b409df1beb5ff6acc2d7842d4f78543d413ebd468bc0171af?s=96&d=mm&r=g","caption":"admin"},"sameAs":["https:\/\/vbastring.com\/blog"],"url":"https:\/\/vbastring.com\/blog\/author\/admin\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/posts\/387","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/comments?post=387"}],"version-history":[{"count":5,"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/posts\/387\/revisions"}],"predecessor-version":[{"id":489,"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/posts\/387\/revisions\/489"}],"wp:attachment":[{"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/media?parent=387"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/categories?post=387"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/tags?post=387"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}