{"id":64183,"date":"2007-08-22T01:39:00","date_gmt":"2007-08-22T01:39:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/08\/22\/how-can-i-copy-data-from-one-spreadsheet-to-another\/"},"modified":"2007-08-22T01:39:00","modified_gmt":"2007-08-22T01:39:00","slug":"how-can-i-copy-data-from-one-spreadsheet-to-another","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-copy-data-from-one-spreadsheet-to-another\/","title":{"rendered":"How Can I Copy Data From One Spreadsheet to Another?"},"content":{"rendered":"<p><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\"> \n<P>Hey, Scripting Guy! How can I copy data from one Excel spreadsheet to another Excel spreadsheet?<BR><BR>&#8212; AC <\/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, AC. You know, we\u2019re going to let you in on a little secret here: the Scripting Guy who writes this column is a tad bit tired this morning. That\u2019s because he arrived home late last night (well after midnight) following a flight from Rome to Seattle. (A flight, in case you\u2019re wondering, that takes just a shade under forever to complete.) And yet, despite being tired from a full day of travel, and despite the effects of jet lag, he still got up at 6:00 this morning and came into work. Why did he come in to work rather than take one more day off? Because he\u2019s a dedicated Microsoft employee who absolutely <I>loves<\/I> his job, that\u2019s why.<\/P>\n<P>Well, that and the fact that he\u2019s trying to stockpile enough vacation days to enable him to take the entire month of December off. But no one needs to know about that, least of all his manager or anyone he works with.<\/P>\n<P>Actually, today\u2019s article should be an interesting one. After all, the Scripting Guy who writes this column often wanders through the hallways of Microsoft, telling anyone who has the misfortune to run into him, \u201cA script that can copy data from one Excel spreadsheet to another? Why, I could write a script like that in my <I>sleep<\/I>!\u201d<\/P>\n<P>So can he <I>really<\/I> write a script like that in his sleep? We\u2019re about to find out:<\/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(&#8220;Sheet1&#8221;)<\/p>\n<p>Set objRange = objWorksheet.Range(&#8220;A1:A20&#8221;)\nobjRange.Copy<\/p>\n<p>Set objExcel2 = CreateObject(&#8220;Excel.Application&#8221;)\nobjExcel2.Visible = True<\/p>\n<p>Set objWorkbook2 = objExcel2.Workbooks.Add\nSet objWorksheet2 = objWorkbook2.Worksheets(&#8220;Sheet1&#8221;)<\/p>\n<p>objWorksheet2.Paste\n<\/PRE>\n<P>OK, not too bad; now let\u2019s see if he can <I>explain<\/I> a script that can copy data from one Excel spreadsheet to another in his sleep. To begin with, the script 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 Microsoft Excel that we can see onscreen. We next use the <B>Open<\/B> method to open the file C:\\Scripts\\Test.xls, then use the following line of code to bind us to the first worksheet in the file:<\/P><PRE class=\"codeSample\">Set objWorksheet = objWorkbook.Worksheets(&#8220;Sheet1&#8221;)\n<\/PRE>\n<P>That was pretty easy, wasn\u2019t it?<\/P>\n<P>Let\u2019s see, what\u2019s next? Well, in order to copy data to the Clipboard, we need to first specify the range of data to be copied (that is, the cells we want to copy), then call the <B>Copy<\/B> method. That\u2019s what these two lines of code are for:<\/P><PRE class=\"codeSample\">Set objRange = objWorksheet.Range(&#8220;A1:A20&#8221;)\nobjRange.Copy\n<\/PRE>\n<P>As you can see, in line one we create an instance of Excel\u2019s <B>Range<\/B> object, in this case a Range object encompassing the cells A1 through A20 (although, needless to say, this could be any range of data we want). In line 2, we call the Copy method, a command that copies cells A1 through A20 (all the cells in our range) to the Clipboard.<\/P>\n<P>You know what? That was pretty easy, too. <\/P>\n<P>But wait; we\u2019re not done yet. After all, we still have to paste this data into a second spreadsheet. For our sample script, we\u2019re going to paste this information into a brand-new spreadsheet; however, we could also open and paste the data into another pre-existing spreadsheet if we so desired. With that in mind, we thus create a new instance of Microsoft Excel, giving this instance the object reference objExcel2 and setting <I>its<\/I> Visible property to True:<\/P><PRE class=\"codeSample\">Set objExcel2 = CreateObject(&#8220;Excel.Application&#8221;)\nobjExcel2.Visible = True\n<\/PRE>\n<P>D\u00e9j\u00e0 vu, huh?<\/P>\n<P>After creating our second instance of Excel, we use the <B>Add<\/B> method to add a new, blank workbook to objExcel2, then use this line of code to bind to the first worksheet in that new workbook:<\/P><PRE class=\"codeSample\">Set objWorksheet2 = objWorkbook2.Worksheets(&#8220;Sheet1&#8221;)\n<\/PRE>\n<P>At this point all we need to do is call the <B>Paste<\/B> method and, by extension, call it a day:<\/P><PRE class=\"codeSample\">objWorksheet2.Paste\n<\/PRE>\n<P>Good observation: <B>Paste<\/B> is actually a method that belongs to the <B>Worksheet<\/B> object (unlike the Copy method, which belongs to the Range object). By default, Excel will paste the data into cell A1 (and adjoining cells as needed). What if we want to paste this data somewhere else? Well, the only way to do that is to make a cell other than cell A1 the active cell. For example, here\u2019s a line of code that makes cell D5 the active cell on our worksheet:<\/P><PRE class=\"codeSample\">objWorksheet2.Range(&#8220;D5&#8221;).Activate\n<\/PRE>\n<P>Now when we call the Paste method the data will be pasted into cell D5 (and any adjoining cells).<\/P>\n<P>By the way, thanks in advance for not telling anyone how tired the Scripting Guy who writes this column really is. The fact of the matter is that if anyone knew the truth, they\u2019d make him go home and, in turn, use up one of his precious vacation days. As it is, right now the Scripting Guy who writes this column is wandering around in a daze, eyes half-closed, and muttering incoherently to himself. Which, considering the fact that this is pretty standard behavior for him anyway, means that no one suspects a thing.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I copy data from one Excel spreadsheet to another Excel spreadsheet?&#8212; AC Hey, AC. You know, we\u2019re going to let you in on a little secret here: the Scripting Guy who writes this column is a tad bit tired this morning. That\u2019s because he arrived home late last night (well [&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-64183","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 copy data from one Excel spreadsheet to another Excel spreadsheet?&#8212; AC Hey, AC. You know, we\u2019re going to let you in on a little secret here: the Scripting Guy who writes this column is a tad bit tired this morning. That\u2019s because he arrived home late last night (well [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64183","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=64183"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64183\/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=64183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=64183"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=64183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}