{"id":316,"date":"2015-11-11T12:56:25","date_gmt":"2015-11-11T12:56:25","guid":{"rendered":"http:\/\/miguelontheweb.com\/wordpress\/?p=316"},"modified":"2015-11-11T12:59:21","modified_gmt":"2015-11-11T12:59:21","slug":"vba-to-prevent-you-from-changing-list-total","status":"publish","type":"post","link":"http:\/\/miguelontheweb.com\/wordpress\/?p=316","title":{"rendered":"VBA To Prevent You From Changing List Total."},"content":{"rendered":"<p>There are times while working with an excel list that you would like to delete items of a\u00a0list without changing the total Sum of that list. Example of this situation often occur when you are reconciling an account and you have various positive and negative values that offset each other. \u00a0In this small article I will illustrate how you can use Excel and VBA to warn you when your cell deletion has cause the total value of a list to change. Lets take a quick look on how you would tackle this.<!--more--><\/p>\n<p>Imagine you had \u00a0the following list in a excel sheet.<\/p>\n<p><a href=\"http:\/\/miguelontheweb.com\/wordpress\/wp-content\/uploads\/2015\/11\/ExcelList1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-322\" src=\"http:\/\/miguelontheweb.com\/wordpress\/wp-content\/uploads\/2015\/11\/ExcelList1-300x154.png\" alt=\"ExcelList\" width=\"300\" height=\"154\" srcset=\"http:\/\/miguelontheweb.com\/wordpress\/wp-content\/uploads\/2015\/11\/ExcelList1-300x154.png 300w, http:\/\/miguelontheweb.com\/wordpress\/wp-content\/uploads\/2015\/11\/ExcelList1-700x359.png 700w, http:\/\/miguelontheweb.com\/wordpress\/wp-content\/uploads\/2015\/11\/ExcelList1.png 804w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>You can tell by looking at this list that you can easily delete the positive 20 and the -20 \u00a0without changing the total sum of the list. In addition, we can also remove the -10 and 10. \u00a0However, what if the list was a list of 100 rows and you would like to continuously remove items without changing the totals. Well that&#8217;s where VBA would come in. The total of this list is 100 as you can tell by looking at cell<strong> B8<\/strong>. I will go into the\u00a0developer tab in Excel and click on Visual Basic. I \u00a0will then find &#8220;Sheet1&#8221; in the VBA project window usually on the left hand side. Double click on Sheet1(Sheet1) to open the editor on the right hand side. From\u00a0the drop down menus on the top right hand side , select &#8220;Worksheet&#8221; and &#8220;SelectionChange&#8221; respectively. \u00a0Finally, add code to the &#8220;<strong>SelectionChange<\/strong>&#8221; event of Sheet1 so that it fires this event every time we change something on this sheet.<\/p>\n<pre class=\"lang:default decode:true \">private Sub Worksheet_SelectionChange(ByVal Target As Range)\r\n\t\r\n\tDim CellValue As String\r\n\t\r\n\tCellValue = Range(\"B8\").Value\r\n\tIf CellValue &lt;&gt; \"100\" Then\r\n\t\tMsgBox (\"OOPS GO BACK\")\r\n\t\t\r\n\tEnd If\r\nEnd Sub\r\n<\/pre>\n<p>We can quickly step through the code, there is not much to it. First we \u00a0cast \u00a0the CellValue \u00a0variable as a string. Than we are assigning the value from cell <strong>B8<\/strong> to the variable CellValue. Finally we are testing the CellValue variable to see if the amount is not equal to the amount we don&#8217;t want to change, in this case 100. Thats all there is to it.<\/p>\n<p>Pretty simple, but helpful,\u00a0\u00a0leave me some comments below. I would love to hear what yout thoughts are on this post.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are times while working with an excel list that you would like to delete items of a\u00a0list without changing the total Sum of that list. Example of this situation often occur when you are reconciling an account and you have various positive and negative values that offset each other. \u00a0In this small article I &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/miguelontheweb.com\/wordpress\/?p=316\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;VBA To Prevent You From Changing List Total.&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[15,14],"class_list":["post-316","post","type-post","status-publish","format-standard","hentry","category-vba","tag-excel","tag-vba"],"_links":{"self":[{"href":"http:\/\/miguelontheweb.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/316","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/miguelontheweb.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/miguelontheweb.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/miguelontheweb.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/miguelontheweb.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=316"}],"version-history":[{"count":8,"href":"http:\/\/miguelontheweb.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/316\/revisions"}],"predecessor-version":[{"id":329,"href":"http:\/\/miguelontheweb.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/316\/revisions\/329"}],"wp:attachment":[{"href":"http:\/\/miguelontheweb.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=316"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/miguelontheweb.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=316"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/miguelontheweb.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=316"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}