{"id":70053,"date":"2005-04-11T15:33:00","date_gmt":"2005-04-11T15:33:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/04\/11\/how-can-i-insert-a-column-into-a-spreadsheet\/"},"modified":"2005-04-11T15:33:00","modified_gmt":"2005-04-11T15:33:00","slug":"how-can-i-insert-a-column-into-a-spreadsheet","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-insert-a-column-into-a-spreadsheet\/","title":{"rendered":"How Can I Insert a Column into a 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 insert a column into an Excel spreadsheet?<BR><BR>&#8212; HE<\/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, HE. Let\u2019s assume you have a spreadsheet that looks something like this:<\/P><IMG border=\"0\" alt=\"Microsoft Excel\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/addcolumn.jpg\" width=\"304\" height=\"229\"> \n<P><BR>And, yes, it <I>is<\/I> a very nice little spreadsheet, except for one problem. As you can see, we goofed, and we forgot to insert Dataset 3; instead, we only have Datasets 1, 2, and 4. Even worse, Dataset 4 is in the wrong spot; it needs to be moved over to column D. In other words, we need to insert a new column between columns B and C. That will move Dataset 4 over to column D (where it belongs), and give us a blank column in column C where we can enter the missing information for Dataset 3.<\/P>\n<P>Make sense? The only question is this: how do we insert a column in a spreadsheet?<\/P>\n<P>Ah, but don\u2019t worry, that\u2019s what the Scripting Guys are here for. Here\u2019s a script that writes a column header to columns A, B, and C, then inserts a new, blank column between columns B and C:<\/P><PRE class=\"codeSample\">Const xlShiftToRight = -4161<\/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>objWorksheet.Cells(1,1) = &#8220;Dataset 1&#8221;\nobjWorksheet.Cells(1,2) = &#8220;Dataset 2&#8221;\nobjWorksheet.Cells(1,3) = &#8220;Dataset 4&#8221;<\/p>\n<p>Set objRange = objExcel.Range(&#8220;C1&#8221;).EntireColumn\nobjRange.Insert(xlShiftToRight)\n<\/PRE>\n<P>The script begins by defining a constant named xlShiftToRight and setting the value to -4161 (yes, <I>minus<\/I> 4161; go figure). To insert a column (or a row) in Excel you select an existing column or row and then move all those cells either to the right (in which case you use the constant xlShiftToRight) or down (in which case you use the constant xlShiftDown, which has a value of -4121). After defining the constant we create an instance of Excel, set the <B>Visible <\/B>property to True, then add a new workbook and a new worksheet.<\/P>\n<P>Next we add a header to cells A1, B1, and C1; this sets the stage for us to actually insert the new column. And we do <I>that<\/I> using just two lines of code: <\/P><PRE class=\"codeSample\">Set objRange = objExcel.Range(&#8220;C1&#8221;).EntireColumn\nobjRange.Insert(xlShiftToRight)\n<\/PRE>\n<P>To begin with, we create a <B>Range<\/B> object representing all the cells in column C, the column we need to shift over. We do that by creating a range consisting of a single cell in column C (in this case, cell C1) and then using the <B>EntireColumn<\/B> property to extend this range to <I>all<\/I> the cells in column C. All we have to do then is call the <B>Insert<\/B> method, passing the constant xlShiftToRight as the sole parameter. This simply tells Excel that we want to take the specified range &#8211; column C &#8211; and move those cells over to the right. And look what happens:<\/P><IMG border=\"0\" alt=\"Microsoft Excel\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/newcolumn.jpg\" width=\"350\" height=\"226\"> \n<P><BR>We\u2019ve successfully inserted a new, blank column, just like we wanted to do.<\/P>\n<P>And before you ask, here\u2019s a similar that scripts that adds a new <I>row<\/I> across the top of a spreadsheet:<\/P><PRE class=\"codeSample\">Const xlShiftDown = -4121<\/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>objWorksheet.Cells(1,1) = &#8220;Dataset 2A&#8221;\nobjWorksheet.Cells(1,2) = &#8220;Dataset 2B&#8221;\nobjWorksheet.Cells(1,3) = &#8220;Dataset 2C&#8221;<\/p>\n<p>Set objRange = objExcel.Range(&#8220;A1&#8221;).EntireRow\nobjRange.Insert(xlShiftDown)\n<\/PRE>\n<P>Cool, huh? If this whets your appetite for 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, which features new Office scripting tips and tricks every Tuesday and Thursday<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I insert a column into an Excel spreadsheet?&#8212; HE Hey, HE. Let\u2019s assume you have a spreadsheet that looks something like this: And, yes, it is a very nice little spreadsheet, except for one problem. As you can see, we goofed, and we forgot to insert Dataset 3; instead, we [&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,711,48,49,3,5],"class_list":["post-70053","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-excel-spreadsheet","tag-excel-application","tag-microsoft-excel","tag-office","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I insert a column into an Excel spreadsheet?&#8212; HE Hey, HE. Let\u2019s assume you have a spreadsheet that looks something like this: And, yes, it is a very nice little spreadsheet, except for one problem. As you can see, we goofed, and we forgot to insert Dataset 3; instead, we [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70053","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=70053"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70053\/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=70053"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70053"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70053"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}