{"id":70133,"date":"2005-03-30T17:01:00","date_gmt":"2005-03-30T17:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/03\/30\/how-can-i-copy-and-rename-a-worksheet-in-the-same-workbook\/"},"modified":"2005-03-30T17:01:00","modified_gmt":"2005-03-30T17:01:00","slug":"how-can-i-copy-and-rename-a-worksheet-in-the-same-workbook","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-copy-and-rename-a-worksheet-in-the-same-workbook\/","title":{"rendered":"How Can I Copy and Rename a Worksheet in the Same Workbook?"},"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! I\u2019d like to copy one of the worksheets in an Excel workbook and then paste that copy into the very same workbook; once it\u2019s pasted it, I\u2019d like to rename it. Can I do that?<BR><BR>&#8212; JG<\/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, JG. Of course you can do this; after all, if you can do something in Excel it\u2019s almost 100% guaranteed that you can do it using a script. This is actually pretty easy, with one little catch we\u2019ll talk about right away.<\/P>\n<P>To begin with, it\u2019s very easy to copy a worksheet in Excel; all you have to do is bind to the worksheet and call the <B>Copy<\/B> method. For example, this script will copy Sheet1 in a workbook named C:\\Scripts\\Testsheet.xls:<\/P><PRE class=\"codeSample\">Set objExcel = CreateObject(&#8220;Excel.Application&#8221;)\nSet objWorkbook = objExcel.Workbooks.Open(&#8220;C:\\Scripts\\Testsheet.xls&#8221;)\nobjExcel.Visible = TRUE<\/p>\n<p>Set objWorksheet = objWorkbook.Worksheets(&#8220;Sheet1&#8221;)\nobjWorksheet.Copy\n<\/PRE>\n<P>As you can see, there\u2019s nothing too fancy here. We create an instance of the <B>Excel.Application<\/B> object and then use the <B>Open<\/B> method to open the workbook C:\\Scripts\\Testsheet.xls. We set the <B>Visible<\/B> property to TRUE (just so we can watch the drama unfold) and create an object reference to Sheet1, the worksheet to be copied:<\/P><PRE class=\"codeSample\">Set objWorksheet = objWorkbook.Worksheets(&#8220;Sheet1&#8221;)\n<\/PRE>\n<P>We then call the <B>Copy<\/B> method to create a copy of the worksheet.<\/P>\n<P>This is the tricky part. The preceding script works, but there\u2019s one problem: by default, the Copy method copies the worksheet into a brand-new workbook. We\u2019ll end up with two copies of Sheet1 alright, but one copy will be in Testsheet.xls and the other copy will be in a new workbook. Not what you had in mind. (If you aren\u2019t sure what we\u2019re talking about, give the script a try and you\u2019ll see what we mean.)<\/P>\n<P>So how do we fix this? Well, what have to do is specify an insertion point for the new worksheet. One way to do that is to indicate which sheet the copied worksheet should be inserted after. To do that, we need to create a second object reference. For example, if we want to insert the new worksheet after Sheet3, we need to create an object reference to Sheet3. Thus:<\/P><PRE class=\"codeSample\">Set objLastSheet = objWorkbook.Worksheets(&#8220;Sheet3&#8221;)\n<\/PRE>\n<P>When we have this second object reference we then pass it as a parameter to the Copy method. This line of code copies Sheet1 and tells Excel to insert the copied worksheet after Sheet3:<\/P><PRE class=\"codeSample\">Set objExcel = CreateObject(&#8220;Excel.Application&#8221;)\nSet objWorkbook = objExcel.Workbooks.Open(&#8220;C:\\Scripts\\testsheet.xls&#8221;)\nobjExcel.Visible = TRUE<\/p>\n<p>Set objLastSheet = objWorkbook.Worksheets(&#8220;Sheet3&#8221;)\nSet objWorksheet = objWorkbook.Worksheets(&#8220;Sheet1&#8221;)\nobjWorksheet.Copy, objLastSheet\n<\/PRE>\n<P>Note what happens when we do this; we get a copy of Sheet1 &#8212; named <B>Sheet1 (2)<\/B> &#8212; inserted immediately after Sheet3, just the way we wanted:<\/P><IMG border=\"0\" alt=\"Microsoft Excel\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/sheetcopy.jpg\" width=\"358\" height=\"314\"> \n<P><BR>Having copied and inserted the new worksheet all that\u2019s left is to rename Sheet1 (2). That takes two lines of code: we create an object reference to the new worksheet and then assign it a new name. Or, to put it programmatically:<\/P><PRE class=\"codeSample\">Set objWorksheet = objWorkbook.Worksheets(&#8220;Sheet1 (2)&#8221;)\nobjWorksheet.Name = &#8220;My New Worksheet&#8221;\n<\/PRE>\n<P>Like we said, once we got past the tricky part, this was almost <I>too<\/I> easy. Here\u2019s a completed script that carries out both tasks for you: it copies Sheet1, and then renames it to <B>My New Worksheet<\/B>:<\/P><PRE class=\"codeSample\">Set objExcel = CreateObject(&#8220;Excel.Application&#8221;)\nSet objWorkbook = objExcel.Workbooks.Open(&#8220;C:\\Scripts\\testsheet.xls&#8221;)\nobjExcel.Visible = TRUE<\/p>\n<p>Set objLastSheet = objWorkbook.Worksheets(&#8220;Sheet3&#8221;)\nSet objWorksheet = objWorkbook.Worksheets(&#8220;Sheet1&#8221;)\nobjWorksheet.Copy, objLastSheet<\/p>\n<p>Set objWorksheet = objWorkbook.Worksheets(&#8220;Sheet1 (2)&#8221;)\nobjWorksheet.Name = &#8220;My New Worksheet&#8221;\n<\/PRE>\n<P>If you\u2019re looking for more tips and tricks for scripting Microsoft Office check out <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/officetips\/default.mspx\"><B>Office Space<\/B><\/A>, published every Tuesday and Thursday in the Script Center.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I\u2019d like to copy one of the worksheets in an Excel workbook and then paste that copy into the very same workbook; once it\u2019s pasted it, I\u2019d like to rename it. Can I do that?&#8212; JG Hey, JG. Of course you can do this; after all, if you can do something in [&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-70133","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! I\u2019d like to copy one of the worksheets in an Excel workbook and then paste that copy into the very same workbook; once it\u2019s pasted it, I\u2019d like to rename it. Can I do that?&#8212; JG Hey, JG. Of course you can do this; after all, if you can do something in [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70133","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=70133"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70133\/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=70133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70133"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}