{"id":65613,"date":"2007-02-01T00:34:00","date_gmt":"2007-02-01T00:34:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/02\/01\/hey-scripting-guy-how-can-i-change-the-font-color-in-office-excel-if-a-specified-condition-is-met\/"},"modified":"2007-02-01T00:34:00","modified_gmt":"2007-02-01T00:34:00","slug":"hey-scripting-guy-how-can-i-change-the-font-color-in-office-excel-if-a-specified-condition-is-met","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-change-the-font-color-in-office-excel-if-a-specified-condition-is-met\/","title":{"rendered":"Hey, Scripting Guy! How Can I Change the Font Color in Office Excel If a Specified Condition is Met?"},"content":{"rendered":"<h2><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Question\" border=\"0\" title=\"Hey, Scripting Guy! Question\" class=\"nearGraphic\" \/> <\/h2>\n<p>Hey, Scripting Guy! In an Office Excel spreadsheet, how can I change the color of the text if the value in column B minus the value in column A is greater than or equal to 10?<\/p>\n<p>&#8212; EP<\/p>\n<p><img decoding=\"async\" height=\"5\" width=\"5\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" alt=\"Spacer\" border=\"0\" \/><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Answer\" border=\"0\" title=\"Hey, Scripting Guy! Answer\" class=\"nearGraphic\" \/><a href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><img decoding=\"async\" height=\"288\" width=\"120\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" align=\"right\" alt=\"Script Center\" border=\"0\" title=\"Script Center\" class=\"farGraphic\" \/><\/a><\/p>\n<p>Hey, EP. You know, before the Scripting Guy who writes this column started writing this column, he vowed that <i>Hey, Scripting Guy!<\/i> would have a laser-like focus on helping people learn system administration scripting; <i>Hey, Scripting Guy! <\/i>would <i>not<\/i> be a forum for inane rantings and ravings that no one else cared about. Throughout the years has he managed to stick to that vow? Let&rsquo;s put it this way: have you seen the <a target=\"_blank\" href=\"https:\/\/www.youtube.com\/watch?v=sPXI5-V9kXE\"><b>commercial featuring the Rock&rsquo;em Sock&rsquo;em Robots<\/b><\/a>? The Scripting Guy who writes this column this column has, and he finds this commercial very distressing.<\/p>\n<p><i>Very<\/i> distressing.<\/p>\n<p>If you haven&rsquo;t seen the commercial, two life-size Rock&rsquo;em Sock&rsquo;em Robots (made of metal, oddly enough) are duking it out in a boxing ring. After defeating the red robot the blue robot crashes through the gymnasium door, terrorizes a couple of people on the street, and then proceeds to attack a pickup truck, a fight he ends up losing.<\/p>\n<p>So what&rsquo;s the big deal here? Well, let&rsquo;s be realistic here: the Rock&rsquo;em Sock&rsquo;em Robots would never terrorize innocent people, nor would they pick a fight with a pickup truck that was just sitting there minding its own business. The Scripting Guy who writes this column had Rock&rsquo;em Sock&rsquo;em Robots when he was a kid; the Scripting Son had Rock&rsquo;em Sock&rsquo;em Robots when <i>he<\/i> was a kid. In all that time those robots never once left the ring, nor did they ever attack anyone other than the other robot. A Rock&rsquo;em Sock&rsquo;em Robot would <i>never<\/i> do anything like that; they have too much class. This is an outrage and something needs to be done about it.<\/p>\n<p>What&rsquo;s that? Are we finished ranting and raving? Sure &hellip; for now. Why do you ask?<\/p>\n<p>Oh, right: a script that can do a little math and then, as needed, change the text color in Microsoft Excel. Hey, all you had to do was ask:<\/p>\n<pre class=\"codeSample\"><pre class=\"codeSample\">Set objExcel = CreateObject(\"Excel.Application\")\nobjExcel.Visible = True\n\nSet objWorkbook = objExcel.Workbooks.Open(\"C:\\Scripts\\Test.xls\")\nSet objWorksheet = objWorkbook.Worksheets(1)\n\ni = 1\n\nDo Until objWorksheet.Cells(i, 1) = \"\"\n    intA = objWorksheet.Cells(i, 1)\n    intB = objWorksheet.Cells(i, 2)\n    If intB - intA &gt;= 10 Then\n        objWorksheet.Cells(i, 1).Font.ColorIndex = 3\n        objWorksheet.Cells(i, 2).Font.ColorIndex = 3\n    End If\n    i = i + 1\nLoop<\/pre>\n<p>According to EP he has a spreadsheet which &ndash; in simplified fashion &ndash; looks something like this:<\/p>\n<p><img decoding=\"async\" height=\"263\" width=\"400\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/redtext1.jpg\" alt=\"Microsoft Excel\" border=\"0\" \/><\/p>\n<p>As you can see, there&rsquo;s a year (e.g., 2002) shown in column A and another year (e.g., 2004) shown in column B. Our task? Subtract column A from column B (2004 &ndash; 2002) and then, if the result is greater than or equal to 10, change the font color for that particular row. Can we do that? Assuming that we don&rsquo;t have to stop for any further rants, you bet we can.<\/p>\n<p>Our script starts in straightforward fashion: we create an instance of the <b>Excel.Application<\/b> object and then set the <b>Visible<\/b> property to True; that gives us a running instance of Excel that we can see onscreen. We then use these two lines of code to open the file C:\\Scripts\\Test.xls and bind to the first worksheet in the file:<\/p>\n<pre class=\"codeSample\">Set objWorkbook = objExcel.Workbooks.Open(\"C:\\Scripts\\Test.xls\")\nSet objWorksheet = objWorkbook.Worksheets(1)<\/pre>\n<p>Once that&rsquo;s done we create a counter variable named i and assign it the value 1; we&rsquo;ll use this variable to keep track of the current row in the worksheet.<\/p>\n<p>Got all that? Good; now it&rsquo;s time to do some <i>real<\/i> scripting. In order to keep matters as simple as we can, we&rsquo;re assuming that the data begins in row 1, column 1; that is, there&rsquo;s no header row. In addition, we&rsquo;re assuming that there are no blanks rows in the data. Based on those assumptions, our next chore is to set up a Do Until loop that runs until we encounter a blank cell in column A; that&rsquo;s what this line of code is for:<\/p>\n<pre class=\"codeSample\">Do Until objWorksheet.Cells(i, 1) = \"\"<\/pre>\n<p>Remember, the counter variable i keeps track of the row for us. When our loop begins, that means we&rsquo;re checking to see if cell row 1, column 1 is blank. If it is, then the script is finished. <\/p>\n<p>If it&rsquo;s not we then take the value of cell row 1, column 1 and assign it to a variable named intA; needless to say, this variable represents the value of cell A1 (row 1, column 1). We then take the value of row 1, column 2 and assign it to a variable named intB; in turn, this variable represents the value of cell B1. That&rsquo;s what these two lines of code do:<\/p>\n<pre class=\"codeSample\">intA = objWorksheet.Cells(i, 1)\nintB = objWorksheet.Cells(i, 2)<\/pre>\n<p>As soon as we have the values in columns A and B we can then use this line of code to determine whether or not the result of subtracting column A from column B is greater than or equal to 10:<\/p>\n<pre class=\"codeSample\">If intB - intA &gt;= 10 Then<\/pre>\n<p>If the value is not greater than or equal to 10 then we don&rsquo;t do anything at all. But what if the value <i>is<\/i> greater than or equal to 10 (as it will be in row 3, when we subtract 1992 from 2004)? In that case, we execute these two lines of code:<\/p>\n<pre class=\"codeSample\">objWorksheet.Cells(i, 1).Font.ColorIndex = 3\nobjWorksheet.Cells(i, 2).Font.ColorIndex = 3<\/pre>\n<p>What&rsquo;s going on here? Well, in the first line we&rsquo;re simply setting the <b>Font.ColorIndex<\/b> property for the cell in column A to red. (A value of 3 means red; for more information on available colors and their values, see <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/officetips\/mar05\/tips0329.mspx\"><b>this Office Space article<\/b><\/a>.) And in the second line &ndash; that&rsquo;s right, in the second line we&rsquo;re setting the <b>Font.ColorIndex<\/b> property for the cell in column B to red. When all is said and done our spreadsheet should look a little something like this:<\/p>\n<p><img decoding=\"async\" height=\"263\" width=\"400\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/redtext2.jpg\" alt=\"Microsoft Excel\" border=\"0\" \/><\/p>\n<p>And there you have it.<\/p>\n<p>Now, getting back to the Rock&rsquo;em Sock&rsquo;em Robots, the Scripting Guy who writes this column recommends that, as a way to register our collective disappointment, we all agree to boycott sushi restaurants. That&rsquo;s a tough thing to do and a tough sacrifice to make. But it&rsquo;s the only way these companies will learn.<\/p>\n<table cellpadding=\"0\" cellspacing=\"0\" class=\"dataTable\" id=\"ESF\">\n<thead><\/thead>\n<tbody>\n<tr valign=\"top\" class=\"record\">\n<td>\n<p class=\"lastInCell\"><b>Note<\/b>. OK, sure, if you want to get picky the commercial has nothing to do with sushi restaurants; instead, it&rsquo;s for a car company. But the Scripting Guy who writes this column can&rsquo;t boycott cars: he lives in Seattle, a city that&rsquo;s pretty much devoid of mass transit. (In Seattle we don&rsquo;t ever build mass transit systems; instead, we just vote on mass transit systems, year after year after year.) By contrast, sushi restaurants are something he <i>can<\/i> boycott, and without any inconvenience to himself whatsoever. And isn&rsquo;t that the best kind of boycott?<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! In an Office Excel spreadsheet, how can I change the color of the text if the value in column B minus the value in column A is greater than or equal to 10? &#8212; EP Hey, EP. You know, before the Scripting Guy who writes this column started writing this column, he [&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":[711,48,49,3,5],"class_list":["post-65613","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-excel-application","tag-microsoft-excel","tag-office","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! In an Office Excel spreadsheet, how can I change the color of the text if the value in column B minus the value in column A is greater than or equal to 10? &#8212; EP Hey, EP. You know, before the Scripting Guy who writes this column started writing this column, he [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65613","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=65613"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65613\/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=65613"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=65613"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=65613"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}