{"id":64983,"date":"2007-04-27T21:30:00","date_gmt":"2007-04-27T21:30:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/04\/27\/how-can-i-assign-a-background-color-to-cells-in-a-spreadsheet-and-then-sum-those-cells\/"},"modified":"2007-04-27T21:30:00","modified_gmt":"2007-04-27T21:30:00","slug":"how-can-i-assign-a-background-color-to-cells-in-a-spreadsheet-and-then-sum-those-cells","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-assign-a-background-color-to-cells-in-a-spreadsheet-and-then-sum-those-cells\/","title":{"rendered":"How Can I Assign a Background Color to Cells in a Spreadsheet and Then \u201cSum\u201d Those Cells?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! How can I assign a background color (based on a specified value) to a spreadsheet cell and then \u201csum\u201d those cells?<BR><BR>&#8212; JC<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, JC. Thanks for the question. And yes, you heard correctly: in his last game the Scripting Son did indeed go 4-for-4, banging out three singles and a double. That outing raised his batting average for the season to an even .500 which, all things considered, isn\u2019t bad at all. <\/P>\n<P>Of course, as the Scripting Dad likes to remind him, a .500 batting average means that the Scripting Son still fails one out every two times he steps to the plate. Although come to think of it, failing only once every two tries is a <I>way<\/I> better percentage than the Scripting Dad has when it comes to writing this column. Hmmm \u2026.<\/P>\n<P>But you\u2019re in luck, JC, and on two accounts. For one, this happens to be one of those rare occasions when we actually <I>do<\/I> have an answer to a question. For another, we have a number of other things we have to do today; that means we can\u2019t spend as much time on meandering and meaningless anecdotes as we usually do. Instead, we\u2019re going to cut to the chase and get right to the script.<\/P>\n<P>Well, in just a minute, that is. Did you know that the phrase \u201ccut to the chase\u201d originated in Hollywood in the 1920s? In the movie world \u201ccut to the chase\u201d meant skipping past the boring parts of the film \u2013 like characters discussing their dreams and aspirations \u2013 and getting to the good stuff: the chase scene. As a general rule we don\u2019t use the phrase \u201ccut to the chase\u201d in relation to <I>Hey, Scripting Guy!<\/I>; after all, if you skipped the boring parts there wouldn\u2019t be much left to the column, would there?<\/P>\n<P>With that in mind, here\u2019s the scripting equivalent of a chase scene, a script that can assign a background color to cells in a spreadsheet and then \u201csum\u201d those cells:<\/P><PRE class=\"codeSample\">Set objExcel = CreateObject(&#8220;Excel.Application&#8221;)\nobjExcel.Visible = True<\/p>\n<p>Set objWorkbook = objExcel.Workbooks.Add()\nSet objWorksheet = objWorkbook.Worksheets(1)<\/p>\n<p>objExcel.Cells(1, 1).Value = &#8220;A&#8221;\nobjExcel.Cells(2, 1).Value = &#8220;B&#8221;\nobjExcel.Cells(3, 1).Value = &#8220;C&#8221;\nobjExcel.Cells(4, 1).Value = &#8220;D&#8221;\nobjExcel.Cells(5, 1).Value = &#8220;E&#8221;\nobjExcel.Cells(6, 1).Value = &#8220;F&#8221;\nobjExcel.Cells(7, 1).Value = &#8220;G&#8221;\nobjExcel.Cells(8, 1).Value = &#8220;H&#8221;<\/p>\n<p>objExcel.Cells(1, 1).Interior.ColorIndex = 7\nobjExcel.Cells(2, 1).Interior.ColorIndex = 8\nobjExcel.Cells(3, 1).Interior.ColorIndex = 9\nobjExcel.Cells(4, 1).Interior.ColorIndex = 10\nobjExcel.Cells(5, 1).Interior.ColorIndex = 7\nobjExcel.Cells(6, 1).Interior.ColorIndex = 7\nobjExcel.Cells(7, 1).Interior.ColorIndex = 8\nobjExcel.Cells(8, 1).Interior.ColorIndex = 10<\/p>\n<p>i = 1<\/p>\n<p>Do Until objExcel.Cells(i, 1).Value = &#8220;&#8221;\n    intColor = objExcel.Cells(i, 1).Interior.ColorIndex<\/p>\n<p>    Select Case intColor\n        Case 7 intSum = intSum + 5\n        Case 8 intSum = intSum + 10\n        Case 9 intSum = intSum + 15\n        Case 10 intSum = intSum + 20\n    End Select<\/p>\n<p>    i = i + 1\nLoop<\/p>\n<p>Wscript.Echo intSum\n<\/PRE>\n<P>To begin with we should note that we took a few liberties here. For example, JC has a script designed to enter a specific value into a cell and then set the background color of that cell based on the value entered. We didn\u2019t bother setting up a fancy system like that; instead, we simply threw some data into some cells and then arbitrarily changed the background color of each cell. But that\u2019s OK: after you see how easy it is to change the background color of a cell, and how easy it is to \u201cread\u201d these background colors, well, you won\u2019t find it too terribly hard to adapt this script as needed.<\/P>\n<P>Which reminds us: did we mention where the term \u201ccut to the chase\u201d came from? We did? In that case, let\u2019s cut to the chase and discuss how the script works. We start out \u2013 in truly dramatic fashion \u2013 by creating an instance of the <B>Excel.Application<\/B> object and then setting the <B>Visible<\/B> property to True; that gives us a running instance of Excel that we can see on screen. We then use these two lines of code to create a new workbook and to bind to the first worksheet in that workbook:<\/P><PRE class=\"codeSample\">Set objWorkbook = objExcel.Workbooks.Add()\nSet objWorksheet = objWorkbook.Worksheets(1)\n<\/PRE>\n<P>Next we have several lines of code that simply enter values into cells A1 through A8. For example, this command sets the value of cell A1 (row 1, column 1) to the letter <I>A<\/I>:<\/P><PRE class=\"codeSample\">objExcel.Cells(1, 1).Value = &#8220;A&#8221;\n<\/PRE>\n<P>And once we\u2019ve assigned values to the cells we then use code similar to this to (again, arbitrarily) change the background color of each cell:<\/P><PRE class=\"codeSample\">objExcel.Cells(1, 1).Interior.ColorIndex = 7\n<\/PRE>\n<P>You can see how easy it is to change the color of a cell: all you have to do is assign the appropriate color integer to the <B>Interior.ColorIndex<\/B> property. The only tricky part here is knowing which integers correspond to which colors; for example, what color <I>is<\/I> 7? Fortunately, you can find out everything you need to know about colors and color values by taking a look at our <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/officetips\/mar05\/tips0329.mspx\"><B>Office Space article<\/B><\/A> on that very topic.<\/P>\n<P>So where are we? Turns out we\u2019re right here, with a spreadsheet that looks like this:<\/P><IMG height=\"291\" alt=\"Microsoft Excel\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/colorsum.jpg\" width=\"286\" border=\"0\"> \n<P><BR>What we want to do now is sum up the cells in the spreadsheet. However, we aren\u2019t going to do that by adding the spreadsheet values; obviously adding A plus B plus C plus etc. wouldn\u2019t make much sense. Instead, we want to determine the background color of the cell, assign that cell a value based on the color, and then add <I>those<\/I> values. Confusing? Don\u2019t worry; we\u2019ll explain it all in detail. And when we\u2019re done with that we\u2019ll talk about our dreams and aspirations.<\/P>\n<P>Before we start reading the individual cells we have a line of code that assigns the value 1 to a counter variable named i; we\u2019ll use this variable to keep track of our position (row) in the spreadsheet. We then set up a Do loop that runs until it encounters a blank cell in column A (at that point we\u2019re assuming we\u2019ve run out of data):<\/P><PRE class=\"codeSample\">Do Until objExcel.Cells(i, 1).Value = &#8220;&#8221;\n<\/PRE>\n<P>The first thing we do inside this loop is determine the background color of the cell and then assign the integer value to a variable named intColor; that\u2019s what we do here:<\/P><PRE class=\"codeSample\">intColor = objExcel.Cells(i, 1).Interior.ColorIndex\n<\/PRE>\n<P>And once we know the background color of the cell we can start doing a little math:<\/P><PRE class=\"codeSample\">Select Case intColor\n    Case 7 intSum = intSum + 5\n    Case 8 intSum = intSum + 10\n    Case 9 intSum = intSum + 15\n    Case 10 intSum = intSum + 20\nEnd Select\n<\/PRE>\n<P>What we\u2019ve done here is set up a Select Case statement based on the value of intColor. In addition, we\u2019ve assigned numeric values to each of the colors used in the spreadsheet. For example, the color 7 (a kind of pinkish-purplish sort of thing) has been assigned the value 5; the color 8 (sort of a cyan \u2026 we think) has been assigned the value 10; etc. Inside our Select Case statement we increment another variable (intSum). How much do we increment this variable by? Well, that depends on the cell color. For example, suppose we have a pinkish-purple cell. In that case intColor will be equal to 7, our script will execute this line of code, and intSum will be incremented by 5:<\/P><PRE class=\"codeSample\">Case 7 intSum = intSum + 5\n<\/PRE>\n<P>Again, nothing too terribly complicated here. If intColor equals 7 then we assign a new value to the variable intSum: the current value of intSum plus 5. The first time through the loop intSum is equal to 0; because of that, when we encounter that pinkish-purplish cell in row 1, column 1 our equation will be 0 + 5 = 5. As a result, intSum will be equal to 5.<\/P>\n<P>Once we exit the Select Case statement we increment our counter variable by 1, then loop around and repeat the process with the cell in row 2, column 1. After we\u2019ve looped through all the cells the script echoes back the dramatic conclusion:<\/P><PRE class=\"codeSample\">90\n<\/PRE>\n<P>Well, OK. But trust us, it\u2019s more exciting to see it in person than it is to read about it.<\/P>\n<P>And that should do it, JC. Like we said, we have several other things to take care of this afternoon, so we\u2019re going to have to cut today\u2019s column short. We apologize that you didn\u2019t get to hear more about the Scripting Son. But don\u2019t worry: as usual, he\u2019ll be playing baseball on into the month of August. No doubt you\u2019ll get to hear <I>plenty<\/I> of stories before the season is done.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I assign a background color (based on a specified value) to a spreadsheet cell and then \u201csum\u201d those cells?&#8212; JC Hey, JC. Thanks for the question. And yes, you heard correctly: in his last game the Scripting Son did indeed go 4-for-4, banging out three singles and a double. That [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[710,48,49,3,5],"class_list":["post-64983","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-excel-spreadsheet","tag-microsoft-excel","tag-office","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I assign a background color (based on a specified value) to a spreadsheet cell and then \u201csum\u201d those cells?&#8212; JC Hey, JC. Thanks for the question. And yes, you heard correctly: in his last game the Scripting Son did indeed go 4-for-4, banging out three singles and a double. That [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64983","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=64983"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64983\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=64983"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=64983"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=64983"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}