{"id":68983,"date":"2005-09-12T19:52:00","date_gmt":"2005-09-12T19:52:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/09\/12\/how-can-i-center-text-in-an-excel-cell\/"},"modified":"2005-09-12T19:52:00","modified_gmt":"2005-09-12T19:52:00","slug":"how-can-i-center-text-in-an-excel-cell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-center-text-in-an-excel-cell\/","title":{"rendered":"How Can I Center Text in an Excel Cell?"},"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 center text in an Excel cell?<BR><BR>&#8212; MV<\/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, MV. You know, every now and then we get asked a question and we think to ourselves, \u201cWow, how many times do you suppose we\u2019ve answered <I>this<\/I> question?\u201d And, of course, when we go to double-check we find out that we\u2019ve <I>never<\/I> answered that question; instead, it just seems like we <I>should<\/I> have answered the question somewhere along the line. We just didn\u2019t remember.<\/P>\n<P>Before you ask, this has nothing to do with the Scripting Guys getting old; in fact, thanks to hard work and clean living none of the Scripting Guys <I>are<\/I> getting old. And those are <I>not<\/I> grey hairs; they\u2019ve just been bleached out by the sun. <\/P>\n<TABLE id=\"EKD\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. Sure it\u2019s tough, but if you\u2019re going to make your home in sun-drenched Seattle then having your hair bleached out &#8211; so much so that your hair begins to <I>look<\/I> grey &#8211; is just one of those things you have to learn to live with.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>At any rate, it turns out that we\u2019ve <I>never<\/I> told people how to center text in an Excel cell. Well, until now, that is:<\/P><PRE class=\"codeSample\">Set objExcel = CreateObject(&#8220;Excel.Application&#8221;)\nobjExcel.Visible = True\nSet objWorkbook = objExcel.Workbooks.Add<\/p>\n<p>Set objWorksheet = objWorkbook.Worksheets(1)<\/p>\n<p>objWorksheet.Cells(1, 1) = &#8220;A&#8221;\nobjWorksheet.Cells(1, 2) = &#8220;B&#8221;\nobjWorksheet.Cells(1, 3) = &#8220;C&#8221;<\/p>\n<p>objWorksheet.Cells(1, 2).HorizontalAlignment = -4108\n<\/PRE>\n<P>At the risk of spoiling the suspense, we\u2019ll tell you that the only line of code you really need to worry about is the last one; the rest of the script simply creates an Excel spreadsheet and adds some data to it. To do that, the script first creates an instance of the <B>Excel.Application<\/B> object and then sets the <B>Visible<\/B> property to True; that gives us a running instance of Excel that\u2019s visible on screen. We use the <B>Add<\/B> method to add a new workbook to our instance of Excel, then use this line of code to bind to the first worksheet in that workbook:<\/P><PRE class=\"codeSample\">Set objWorksheet = objWorkbook.Worksheets(1)\n<\/PRE>\n<P>Of course, a script that centers text in a cell isn\u2019t all that impressive unless you actually have some text in that cell. Therefore, we use these three lines of code to put the letters <I>A<\/I>, <I>B<\/I>, and <I>C<\/I> into cells A1, B1, and C1, respectively:<\/P><PRE class=\"codeSample\">objWorksheet.Cells(1, 1) = &#8220;A&#8221;\nobjWorksheet.Cells(1, 2) = &#8220;B&#8221;\nobjWorksheet.Cells(1, 3) = &#8220;C&#8221;\n<\/PRE>\n<P>In case you\u2019re wondering, that gives us 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\/centered1.jpg\" width=\"403\" height=\"325\"> \n<P><BR>Now how do we center the text in one of the cells? Well, this line of code centers the text for cell B1 (that is, cell row 1, column 2):<\/P><PRE class=\"codeSample\">objWorksheet.Cells(1, 2).HorizontalAlignment = -4108\n<\/PRE>\n<P>As you can see, all we have to do is set the value of the <B>HorizontalAlignment<\/B> property of the cell to -4108; that will center the text in the cell. If we wanted the cell to be right-aligned, we\u2019d set HorizontalValue to -4152; set the value to -4131 to left-align the text.<\/P>\n<P>Run the script, and the finished spreadsheet will look like this, with the text in cell B1 centered:<\/P><IMG border=\"0\" alt=\"Microsoft Excel\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/centered2.jpg\" width=\"403\" height=\"325\"> \n<P><BR>Another day, another question answered. Speaking of which, how many times <I>have<\/I> we answered a question about centering text in Excel? <\/P>\n<P>Oh, right: once. The sun appears to be bleaching out our brains as well as our hair \u2026.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I center text in an Excel cell?&#8212; MV Hey, MV. You know, every now and then we get asked a question and we think to ourselves, \u201cWow, how many times do you suppose we\u2019ve answered this question?\u201d And, of course, when we go to double-check we find out that we\u2019ve [&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-68983","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 center text in an Excel cell?&#8212; MV Hey, MV. You know, every now and then we get asked a question and we think to ourselves, \u201cWow, how many times do you suppose we\u2019ve answered this question?\u201d And, of course, when we go to double-check we find out that we\u2019ve [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68983","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=68983"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68983\/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=68983"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68983"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68983"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}