{"id":9461,"date":"2014-07-03T12:55:07","date_gmt":"2014-07-03T20:55:07","guid":{"rendered":"http:\/\/www.perivision.net\/wordpress\/?p=9461"},"modified":"2014-07-03T12:55:07","modified_gmt":"2014-07-03T20:55:07","slug":"how-to-update-a-cell-based-on-date-in-excel-and-store-that-value","status":"publish","type":"post","link":"https:\/\/www.perivision.net\/wordpress\/2014\/07\/how-to-update-a-cell-based-on-date-in-excel-and-store-that-value\/","title":{"rendered":"How to update a cell based on date in excel and store that value"},"content":{"rendered":"<p><a href=\"https:\/\/www.perivision.net\/wordpress\/wp-content\/uploads\/2014\/07\/Excel-change-on-Date-Example.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignright size-full wp-image-9465\" src=\"https:\/\/www.perivision.net\/wordpress\/wp-content\/uploads\/2014\/07\/Excel-change-on-Date-Example.jpg\" alt=\"Excel change on Date Example\" width=\"268\" height=\"231\" \/><\/a>A few weeks back I was asked to create a rule in Excel that would recalculate a cell&#8217;s value ONLY on Monday!\u00a0 And store that calculation. What?\u00a0 Hmm..\u00a0 Then I thought, wait a minute, I should be able to do this in the cells directly. At first I tried to do this all in one cell and its got pretty messy. So instead I divided it up into two steps. First convert the date to string, then a simple if statement if the string = &#8220;Mon&#8221;.<br \/>\nTo start, put a number in cell A1. Any number. place 6\/2\/2014 in B2. Place a number in A8 and B8. I have these lower for a reason you will see later.<\/p>\n<p>So first the date. In cell B2 I have the following.<br \/>\n<em>6\/2\/2014<\/em><br \/>\nIn cell B3 I wrote this..<br \/>\n<em>=TEXT(B2,&#8221;ddd&#8221;)<\/em> &#8211; (in the real sheet, this cell is hidden somewhere.<br \/>\nOk, now I do a simple =IF statement. For those of you who do not know, here is how a simple =IF statement works.. IF(logical_test,value_if_true,value_if_false)<\/p>\n<p>So in cell C8 I have the following<br \/>\n<em>=IF(B3=&#8221;Mon&#8221;, A8, B8)<\/em><\/p>\n<p>B3 is the string value of the date from B2. A8 is the value if its Monday, and B8 is the value of its not. Done!<\/p>\n<p>But there is a problem here&#8230; The actual goal is to have the new value STAY as the new value on and after Monday until the following Monday when it gets re calculated again. Ahhh. That is MUCH harder.<\/p>\n<p>So here is where doing calculation in cells break down. Because we want to retain the new value after Monday, we cannot just have a simple =IF test because on tuesday, it goes back to the old value again. the IF statement has two conditions, true or false. We need to find a way to keep the new value we calculated on Monday throughout the week and then update it again the following Monday.<\/p>\n<p>So VBScript to the rescue. If you have never written a script in Excel you may want to look up a few tutorials, its pretty powerful once you get into it, however, lets just walk you through it.<\/p>\n<p>First right-click on the sheet tab in your spreadsheet. In my case, I right-clicked on &#8216;Sheet1&#8217;<\/p>\n<p>A new window will popup called Microsoft Visual Basic for Applications.<\/p>\n<p>Copy and paste the following code in. We will walk through it in a sec..<\/p>\n<blockquote>\n<pre>Private Sub Worksheet_Change(ByVal Target As Range)\r\nDim Last As Integer\r\nApplication.EnableEvents = False\r\n\r\nDim currDateString As String\r\ncurrDateString = Range(\"b3\").Text\r\nIf currDateString = \"Mon\" Then Range(\"C1\") = Range(\"A1\")\r\n\r\nApplication.EnableEvents = True\r\nEnd Sub\r\n<\/pre>\n<\/blockquote>\n<p>This is pretty much the same thing we did with the cells earlier, however, we are going to use the magic command &#8216;Application.EnableEvents = False&#8217;. What this does is still Excel not to calculate anything until told otherwise. Now, lets run our if statement. Again we have this broken up into two steps.<\/p>\n<blockquote>\n<pre>Dim currDateString As String\r\ncurrDateString = Range(\"b3\").Text<\/pre>\n<\/blockquote>\n<p>What these two lines do is set currDateString as a varible and then assign it the string value we already have in B3. If we wanted to be fancy we could have done this in code as well, but I&#8217;m trying to keep this simple.<\/p>\n<p>Next we run our simple If statement.<\/p>\n<blockquote>\n<pre>If currDateString = \"Mon\" Then Range(\"C1\") = Range(\"A1\")<\/pre>\n<\/blockquote>\n<p>This is pretty stright forward, if currDateString (B3) = &#8220;Mon&#8221;, then have the cell C1 = A1.<\/p>\n<p>And we are done. We turn on &#8216;Application.EnableEvents = True&#8217; and let Excel do its thing.<\/p>\n<p>Give it a try. Change A1 while the date is set to June 2 and you will see C1 updating. Change the date, and C1 no longer updates. Close the file and then reopen and the value are where you left off.<\/p>\n<p>Neat eh?<\/p>\n<p>I&#8217;m sure there are cooler ways to write this but again, I was going for simple here.<\/p>\n<p>Enjoy!<\/p>\n<div class=\"SPOSTARBUST-Related-Posts\"><H3>Related Posts<\/H3><ul class=\"entry-meta\"><li class=\"SPOSTARBUST-Related-Post\"><a title=\"How to fix minor rim scrape on a Tesla 3 and the 3-6-9 rule\" href=\"https:\/\/www.perivision.net\/wordpress\/2018\/05\/how-to-fix-minor-rim-scrape-on-a-tesla-3-and-the-3-6-9-rule\/\" rel=\"bookmark\">How to fix minor rim scrape on a Tesla 3 and the 3-6-9 rule<\/a><\/li>\n<li class=\"SPOSTARBUST-Related-Post\"><a title=\"Now thieves are use Amps to steal your car. Now you need a Faraday cage for your key fob\" href=\"https:\/\/www.perivision.net\/wordpress\/2015\/04\/now-thieves-are-use-amps-to-steal-your-car-now-you-need-a-faraday-cage-for-your-key-fob\/\" rel=\"bookmark\">Now thieves are use Amps to steal your car. Now you need a Faraday cage for your key fob<\/a><\/li>\n<li class=\"SPOSTARBUST-Related-Post\"><a title=\"How to put your iphone in developer mode to install Pebble apps\" href=\"https:\/\/www.perivision.net\/wordpress\/2014\/10\/how-to-put-your-iphone-in-developer-mode-to-install-pebble-apps\/\" rel=\"bookmark\">How to put your iphone in developer mode to install Pebble apps<\/a><\/li>\n<li class=\"SPOSTARBUST-Related-Post\"><a title=\"How to fix a slow USB 3 port on the Yoga 13 ideapad or Thinkpad T430\" href=\"https:\/\/www.perivision.net\/wordpress\/2014\/09\/how-to-fix-a-slow-usb-3-port-on-the-yoga-13-ideapad-or-thinkpad-t430\/\" rel=\"bookmark\">How to fix a slow USB 3 port on the Yoga 13 ideapad or Thinkpad T430<\/a><\/li>\n<li class=\"SPOSTARBUST-Related-Post\"><a title=\"How to toggle between 2 or more apps in Android using Tasker geared for Pebble users.\" href=\"https:\/\/www.perivision.net\/wordpress\/2014\/05\/how-to-toggle-between-2-or-more-apps-in-android-using-tasker-geared-for-pebble-users\/\" rel=\"bookmark\">How to toggle between 2 or more apps in Android using Tasker geared for Pebble users.<\/a><\/li>\n<\/ul><\/div>","protected":false},"excerpt":{"rendered":"<p>A few weeks back I was asked to create a rule in Excel that would recalculate a cell&#8217;s value ONLY on Monday!\u00a0 And store that calculation. What?\u00a0 Hmm..\u00a0 Then I thought, wait a minute, I should be able to do this in the cells directly. At first I tried to do this all in one&hellip; <a class=\"read-more\" href=\"https:\/\/www.perivision.net\/wordpress\/2014\/07\/how-to-update-a-cell-based-on-date-in-excel-and-store-that-value\/\">Read More<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1922,59],"tags":[2192,1129,177,2194,2193,2191],"class_list":["post-9461","post","type-post","status-publish","format-standard","hentry","category-how-to-2","category-tips","tag-example","tag-excel","tag-how-to","tag-ms-excel","tag-udpate-cell","tag-vbscript"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pjzQD-2sB","_links":{"self":[{"href":"https:\/\/www.perivision.net\/wordpress\/wp-json\/wp\/v2\/posts\/9461","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.perivision.net\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.perivision.net\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.perivision.net\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.perivision.net\/wordpress\/wp-json\/wp\/v2\/comments?post=9461"}],"version-history":[{"count":4,"href":"https:\/\/www.perivision.net\/wordpress\/wp-json\/wp\/v2\/posts\/9461\/revisions"}],"predecessor-version":[{"id":9466,"href":"https:\/\/www.perivision.net\/wordpress\/wp-json\/wp\/v2\/posts\/9461\/revisions\/9466"}],"wp:attachment":[{"href":"https:\/\/www.perivision.net\/wordpress\/wp-json\/wp\/v2\/media?parent=9461"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.perivision.net\/wordpress\/wp-json\/wp\/v2\/categories?post=9461"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.perivision.net\/wordpress\/wp-json\/wp\/v2\/tags?post=9461"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}