{"id":68043,"date":"2006-02-02T11:45:00","date_gmt":"2006-02-02T11:45:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/02\/02\/how-can-i-remove-all-the-formatting-from-an-excel-spreadsheet\/"},"modified":"2006-02-02T11:45:00","modified_gmt":"2006-02-02T11:45:00","slug":"how-can-i-remove-all-the-formatting-from-an-excel-spreadsheet","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-remove-all-the-formatting-from-an-excel-spreadsheet\/","title":{"rendered":"How Can I Remove All the Formatting from an Excel Spreadsheet?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! How can I remove all the formatting from an Excel spreadsheet?<BR><BR>&#8212; MT<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, MT. You know, several years ago one of the Scripting Guys and his family went to the Blizzard Beach water park in Orlando. While there this Scripting Guy went down the Summit Plummet, reputed at the time to be the highest and fastest water slide in the world. What this Scripting Guy (who was dared by his son to go down the slide, leaving him little choice in the matter) remembers most about the experience is that he stood in line for about 45 minutes and then listened to a minute or so of safety instructions, all for the chance to experience a ride that maybe lasted 3 or 4 seconds, max. <\/P>\n<P>On the bright side, though, no Scripting Son could call him chicken. Or at least not when it came to water slides.<\/P>\n<P>We were reminded of this kind of experience &#8211; a huge buildup for something that was over before you knew it &#8211; as we sat down to write a script that addressed your question. That\u2019s because it didn\u2019t make much sense to show you how to clear all the formatting from a spreadsheet unless we had a spreadsheet that included several different types of formatting. Therefore we wrote a bunch of code to create a formatted spreadsheet. And then, when the time came, we found out that we needed just one line of code to clear all that formatting.<\/P>\n<P>Talk about anti-climactic.<\/P>\n<P>Here\u2019s what the script looks like:<\/P><PRE class=\"codeSample\">Const xlHAlignCenter = -4108<\/p>\n<p>Set objExcel = CreateObject(&#8220;Excel.Application&#8221;)\nobjExcel.Visible = True\nSet objWorkbook = objExcel.Workbooks.Add()\nSet objWorksheet = objWorkbook.Worksheets(1)<\/p>\n<p>For i = 1 to 14\n    objExcel.Cells(i, 1).Value = i\n    objExcel.Cells(i, 2).Interior.ColorIndex = i\nNext<\/p>\n<p>Set objRange = objWorksheet.UsedRange\nobjRange.HorizontalAlignment = xlHAlignCenter \nobjRange.Font.Bold = True<\/p>\n<p>Wscript.Sleep 5000<\/p>\n<p>objRange.ClearFormats\n<\/PRE>\n<P>We should note that we\u2019re going to ignore most of the lines of code in this script; that\u2019s because we\u2019ve included those lines simply to give us a formatted Excel spreadsheet to work with. If you\u2019d like to know more about scripting with Microsoft Excel you might want to take a peek at the <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/officetips\/archive.mspx\"><B>Office Space archive<\/B><\/A>; there you\u2019ll find links to all sorts of articles that show you how to script with Excel.<\/P>\n<P>However, we will at least give you a brief summary of what the script does. To begin with, we create an instance of Excel and then create a new workbook and a new worksheet. We add numbers and change the background color of a bunch of cells on that worksheet, then run smack-dab into this line of code:<\/P><PRE class=\"codeSample\">Set objRange = objWorksheet.UsedRange\n<\/PRE>\n<P>Our goal today is to write a script that removes all the formatting from the entire spreadsheet; as you might expect, the easiest way to do that is to first select the entire spreadsheet. That\u2019s what the <B>UsedRange<\/B> does: it gives us a <B>Range<\/B> object that encompasses the, well, used portion of the spreadsheet. For example, suppose we have data in cell A1 and data in cell G37; the used range consists of cells A1 and G37, as well as all the cells in between. Obviously a quick and easy way to grab all the \u201cused\u201d cells.<\/P>\n<P>Once we have this range we take advantage of it right away, using these two lines of code to boldface and center all the text on the spreadsheet:<\/P><PRE class=\"codeSample\">objRange.HorizontalAlignment = xlHAlignCenter \nobjRange.Font.Bold = True\n<\/PRE>\n<P>That just adds a little more formatting to the mix. Next we pause the script for 5 seconds (5,000 milliseconds). We don\u2019t <I>have<\/I> to do that, but it gives you a chance to gaze at all the pretty formatting, right before the script comes in and removes it all.<\/P>\n<P>And so how <I>do<\/I> we remove all that formatting? Here\u2019s how:<\/P><PRE class=\"codeSample\">objRange.ClearFormats\n<\/PRE>\n<P>See: we weren\u2019t kidding: one line of code. Using our Range object (the one that encompasses the used range) all we have to do is call the <B>ClearFormats<\/B> method and all formatting will be removed. Hey, we <I>told<\/I> you it was a bit anti-climactic. On the bright side, though, if you run this script no one will be able to call you chicken; at least not when it comes to removing all the formatting from an Excel spreadsheet.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I remove all the formatting from an Excel spreadsheet?&#8212; MT Hey, MT. You know, several years ago one of the Scripting Guys and his family went to the Blizzard Beach water park in Orlando. While there this Scripting Guy went down the Summit Plummet, reputed at the time to be [&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":[48,49,3,5],"class_list":["post-68043","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-microsoft-excel","tag-office","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I remove all the formatting from an Excel spreadsheet?&#8212; MT Hey, MT. You know, several years ago one of the Scripting Guys and his family went to the Blizzard Beach water park in Orlando. While there this Scripting Guy went down the Summit Plummet, reputed at the time to be [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68043","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=68043"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68043\/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=68043"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68043"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68043"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}