{"id":69583,"date":"2005-06-16T13:05:00","date_gmt":"2005-06-16T13:05:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/06\/16\/how-can-i-delete-data-from-a-spreadsheet-yet-keep-all-the-formatting\/"},"modified":"2005-06-16T13:05:00","modified_gmt":"2005-06-16T13:05:00","slug":"how-can-i-delete-data-from-a-spreadsheet-yet-keep-all-the-formatting","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-delete-data-from-a-spreadsheet-yet-keep-all-the-formatting\/","title":{"rendered":"How Can I Delete Data from a Spreadsheet Yet Keep All the Formatting?"},"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 delete data from a spreadsheet yet keep all the formatting?<BR><BR>&#8212; TB<\/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, TB. From your email it sounds like you have a script that periodically replaces data in a spreadsheet with new, up-to-date values. The spreadsheet has been preformatted, and what you\u2019d like to do is delete all the old values &#8211; thus making room for new values &#8211; but without deleting any of the formatting. That way you can simply plug in the new values without having to redo the fancy formatting.<\/P>\n<P>We hope it doesn\u2019t look <I>exactly<\/I> like this, but for now we\u2019ll pretend your spreadsheet has the same kind of nifty formatting shown here:<\/P><IMG border=\"0\" alt=\"Microsoft Excel\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/format1.jpg\" width=\"373\" height=\"387\"> \n<P><BR>So can we delete the data yet leave all this\u2026interesting\u2026formatting behind? Of course we can:<\/P><PRE class=\"codeSample\">Set objExcel = CreateObject(&#8220;Excel.Application&#8221;)\nobjExcel.Visible = True<\/p>\n<p>Set objWorkbook = objExcel.Workbooks.Open(&#8220;C:\\Scripts\\Test.xls&#8221;)\nSet objWorksheet = objWorkbook.Worksheets(1)<\/p>\n<p>objWorksheet.UsedRange.ClearContents\n<\/PRE>\n<P>This simple little script starts off by creating an instance of the <B>Excel.Application<\/B> object and then sets the <B>Visible<\/B> property to True (we set Visible to True just so we can see the changes take place on screen). Next we use these two lines of code to open the file C:\\Scripts\\Test.xls and create an object reference to Sheet1, the first worksheet in the file:<\/P><PRE class=\"codeSample\">Set objWorkbook = objExcel.Workbooks.Open(&#8220;C:\\Scripts\\Test.xls&#8221;)\nSet objWorksheet = objWorkbook.Worksheets(1)\n<\/PRE>\n<P>To delete the values without affecting the formatting takes just one line of code:<\/P><PRE class=\"codeSample\">objWorksheet.UsedRange.ClearContents\n<\/PRE>\n<P>All we\u2019re doing here is taking <B>UsedRange<\/B> (which represents all the cells that have data in them) and calling the <B>ClearContents<\/B> method. As you might have guessed, ClearContents will delete the data but leave the formatting alone. When we run the script we\u2019ll end up with a spreadsheet that looks like this:<\/P><IMG border=\"0\" alt=\"Microsoft Excel\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/format2.jpg\" width=\"373\" height=\"387\"> \n<P><BR>Admittedly it\u2019s not the most impressive spreadsheet we\u2019ve ever seen, either. But look what happens when you add data to a cell:<\/P><IMG border=\"0\" alt=\"Microsoft Excel\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/format3.jpg\" width=\"373\" height=\"387\"> \n<P><BR>As you can see, all the old formats have been retained. All we have to do is add the data and our spreadsheet will be just as pretty as ever. And as Martha Stewart would say, that\u2019s a good thing. <\/P>\n<P>OK, so she probably wouldn\u2019t say that if she saw the way we formatted <I>this<\/I> spreadsheet. But you know what we mean.<\/P>\n<P>By the way, if you\u2019re interested in Microsoft Office Scripting be sure and check out the <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/officetips\/default.mspx\"><B>Office Space<\/B><\/A> column, published every Tuesday and Thursday in the Script Center.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I delete data from a spreadsheet yet keep all the formatting?&#8212; TB Hey, TB. From your email it sounds like you have a script that periodically replaces data in a spreadsheet with new, up-to-date values. The spreadsheet has been preformatted, and what you\u2019d like to do is delete all the [&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-69583","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 delete data from a spreadsheet yet keep all the formatting?&#8212; TB Hey, TB. From your email it sounds like you have a script that periodically replaces data in a spreadsheet with new, up-to-date values. The spreadsheet has been preformatted, and what you\u2019d like to do is delete all the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69583","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=69583"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69583\/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=69583"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=69583"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=69583"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}