{"id":65483,"date":"2007-02-20T00:57:00","date_gmt":"2007-02-20T00:57:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/02\/20\/how-can-i-specify-the-number-of-decimal-places-to-display-in-an-excel-spreadsheet\/"},"modified":"2007-02-20T00:57:00","modified_gmt":"2007-02-20T00:57:00","slug":"how-can-i-specify-the-number-of-decimal-places-to-display-in-an-excel-spreadsheet","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-specify-the-number-of-decimal-places-to-display-in-an-excel-spreadsheet\/","title":{"rendered":"How Can I Specify the Number of Decimal Places to Display in an Excel Spreadsheet?"},"content":{"rendered":"<p><H2><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\"> <\/H2>\n<P>Hey, Scripting Guy! How can I specify the number of decimal places to be displayed in an Excel spreadsheet?<BR><BR>&#8212; JS<\/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, JS. Yes, it <I>is<\/I> Presidents Day here in the US, a day on which schools, banks, and government offices are all closed. Fortunately, though, the Scripting Guys are open and ready for business.<\/P>\n<P><I>Why<\/I> are the Scripting Guys open and ready for business on a national holiday? You know, we never thought about that. Hmmm \u2026.<\/P>\n<P>Interestingly enough, JS, Presidents Day is really supposed to mark the birthday of George Washington, the first US President. Because of that, the holiday used to be celebrated on February 22<SUP>nd<\/SUP>, which is Washington\u2019s actual birthday. Many years ago, however, the US Congress voted to move the holiday to the third Monday in February, and to rename it Presidents Day. Is that because the US has had lots and lots of outstanding Presidents, each one as deserving of a national holiday as George Washington? <\/P>\n<P>Uh, maybe we should just leave that question unanswered for now. William Howard Taft, who weighed over 300 pounds, once got stuck in the White House bathtub. But we\u2019re not sure if that really merits a national holiday. <\/P>\n<P>Oh, wait: and General Zachary Taylor, old \u201cRough and Ready\u201d himself, once loaned us a script that lets you specify the number of decimal places to be displayed in an Excel spreadsheet:<\/P><PRE class=\"codeSample\">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 10\n    objExcel.Cells(i, 1).Value = i\/6\nNext<\/p>\n<p>Set objRange = objWorksheet.UsedRange\nobjRange.NumberFormat = &#8220;#.0000&#8221;\n<\/PRE>\n<P>How does this script work? Good question. Although Zachary Taylor died in 1850, Scripting Guy Peter Costantini held a s\u00e9ance for us and managed to contact the late President. Here\u2019s what President Taylor had to say:<\/P>\n<P>Well, sir, I start off by creating an instance of the <B>Excel.Application<\/B> object (crackin\u2019 good object, Excel.Application) and then set the <B>Visible<\/B> property to True. That gives us a running instance of Excel that we can view on the screen. I then use these two lines of code to add a new workbook to our instance of Excel, and to bind us 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>If that don\u2019t beat all, huh? Of course, a spreadsheet without data isn\u2019t much fun now, is it? We need some numbers to work with, so I stuck in this little For Next loop that simply takes the number 1 through 10, divides the number by 6, then puts each quotient in a separate cell in column A:<\/P><PRE class=\"codeSample\">For i = 1 to 10\n    objExcel.Cells(i, 1).Value = i\/6\nNext\n<\/PRE>\n<P>That\u2019s gonna give us a spreadsheet that looks like this:<\/P><IMG height=\"383\" alt=\"Microsoft Excel\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/decimals1.jpg\" width=\"318\" border=\"0\"> \n<P><BR>Now, I know what you\u2019re thinking. You\u2019re thinking, \u201cTarnation, Zachary, that ain\u2019t no good; that spreadsheet ain\u2019t fit for man nor beast. Can\u2019t we format those cells so they all display 4 decimal places?\u201d<\/P>\n<P>Easy does it now; just hold on to your hats. Yes, we can format those cells so that they all display 4 decimal places; why else would I put in these two lines of code:<\/P><PRE class=\"codeSample\">Set objRange = objWorksheet.UsedRange\nobjRange.NumberFormat = &#8220;#.0000&#8221;\n<\/PRE>\n<P>As you can see, in the first line we create an object reference (objRange) to the worksheet\u2019s \u201cused range.\u201d What\u2019s a used range? Well, sir, it\u2019s just what the name says it is: the used range represents the range of cells that have data in them (that is, the cells that have been used). In our sample spreadsheet we have data in cells A1 through A10, so guess what: our used range is going to encompass those very same cells, cells A1 through A10.<\/P>\n<P>And then in the second line of code we specify the number of decimal places for the cells in that range. We want to display four decimal places, so we put 4 zeroes after the decimal point when assigning a value to the <B>NumberFormat<\/B> property. In turn, that\u2019s gonna give us a spreadsheet that looks like this:<\/P><IMG height=\"383\" alt=\"Microsoft Excel\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/decimals2.jpg\" width=\"318\" border=\"0\"> \n<P><BR>Now <I>that\u2019s<\/I> the cat\u2019s pajamas.<\/P>\n<P>What\u2019s that? You say you\u2019d prefer to display <I>five <\/I>decimal places? Boy, there\u2019s just no pleasing some folks, is there? But that\u2019s OK; just put five zeroes after the decimal point, like so:<\/P><PRE class=\"codeSample\">objRange.NumberFormat = &#8220;#.00000&#8221;\n<\/PRE>\n<P>Want to put a leading zero before the decimal point? Then just change the # sign to a 0, like this:<\/P><PRE class=\"codeSample\">objRange.NumberFormat = &#8220;0.00000&#8221;\n<\/PRE>\n<P>I tell you, I could sit here and do this all day. Or at least I could, except for the fact that they serve dinner early up here in heaven, and tonight we\u2019re having johnnycakes and cornpone. Good day to you all.<\/P>\n<P>So there you have it, JS: Zachary Taylor\u2019s take on specifying the number of decimal places to display in an Excel spreadsheet. You know, the interesting thing about having a s\u00e9ance with President Taylor was the fact that \u2013 what\u2019s that? No, we really did, we \u2026 that is, Peter, he \u2013 oh, OK, we admit it: we didn\u2019t really have a s\u00e9ance with Zachary Taylor; we made the whole thing up. And no US President gave us this script; we wrote it ourselves. The Scripting Guys apologize for any misconceptions we may have created here. Sorry.<\/P>\n<P>We\u2019re curious, though: how did you <I>know<\/I> we were making that up? Ah, good point: implying that a US President would make it to heaven <I>was<\/I> kind of a dead giveaway, wasn\u2019t it? Such a silly mistake.<\/P>\n<TABLE class=\"dataTable\" id=\"EHF\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note to US Presidents<\/B>. Come on, guys; we\u2019re just kidding around. Listen, would it help if we gave you a <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/games\/games07\/bobble.mspx\"><B>Dr. Scripto bobblehead doll<\/B><\/A>? If so, well, there\u2019s still plenty of time to enter the <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/games\/default.mspx\"><B>2007 Winter Scripting Games<\/B><\/A>. Remember, all you have to do is enter a single event and you\u2019ll have a chance to win one of 250 Dr. Scripto bobbleheads or a copy of the book <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/msh\/payette1.mspx\"><B>Windows PowerShell in Action<\/B><\/A>. Those are pretty good prizes, even for a US President.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<TABLE class=\"dataTable\" id=\"EEG\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Editor\u2019s Note<\/B>. After a week of late nights scoring events for the <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/games\/default.mspx\"><B>2007 Scripting Games<\/B><\/A>, the lack of sleep has apparently made the Scripting Guy who writes this column even more disagreeable than usual. We\u2019ll throw a few donuts into his office to try to keep him under control for the rest of the week, but we apologize in advance to everyone he manages to insult this week.<\/P><\/TD><\/TR><\/TBODY><\/TABLE><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I specify the number of decimal places to be displayed in an Excel spreadsheet?&#8212; JS Hey, JS. Yes, it is Presidents Day here in the US, a day on which schools, banks, and government offices are all closed. Fortunately, though, the Scripting Guys are open and ready for business. Why [&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-65483","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 specify the number of decimal places to be displayed in an Excel spreadsheet?&#8212; JS Hey, JS. Yes, it is Presidents Day here in the US, a day on which schools, banks, and government offices are all closed. Fortunately, though, the Scripting Guys are open and ready for business. Why [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65483","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=65483"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65483\/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=65483"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=65483"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=65483"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}