{"id":63463,"date":"2007-12-05T01:02:00","date_gmt":"2007-12-05T01:02:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/12\/05\/hey-scripting-guy-how-can-i-cut-a-row-from-one-office-excel-spreadsheet-and-paste-that-row-into-another-spreadsheet\/"},"modified":"2007-12-05T01:02:00","modified_gmt":"2007-12-05T01:02:00","slug":"hey-scripting-guy-how-can-i-cut-a-row-from-one-office-excel-spreadsheet-and-paste-that-row-into-another-spreadsheet","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-cut-a-row-from-one-office-excel-spreadsheet-and-paste-that-row-into-another-spreadsheet\/","title":{"rendered":"Hey, Scripting Guy! How Can I Cut a Row From One Office Excel Spreadsheet and Paste That Row Into Another Spreadsheet?"},"content":{"rendered":"<p><H2><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\"> <\/H2>\n<P>Hey, Scripting Guy! How can I cut a row from an Office Excel spreadsheet and paste it into the first unused row in another Excel spreadsheet?<BR><BR>&#8212; NC<\/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, NC. We apologize if today\u2019s column feels a little damp; as it turns out, those of us in the Seattle area are experiencing another one of the Puget Sound\u2019s \u2026 delightful \u2026 shifts in the weather. Saturday afternoon we got hit with a bucketful of snow; at the Scripting House the snow was piled at least 6 inches high on the railing of the deck. By Sunday morning most of that snow had melted, just in time for another big bucketful to hit. Midway through <I>that<\/I> blizzard the temperature suddenly shot up about 15 degrees; in fact, currently it\u2019s 58 degrees Fahrenheit, which, unofficially, ties the mark for the warmest December 3<SUP>rd<\/SUP> on record.<\/P>\n<P>Oh, and once the temperature shot up it began to rain. And rain. And rain. It began raining \u2013 hard \u2013 about 10:00 AM Sunday morning and, as of 2:30 PM Monday afternoon, it hadn\u2019t stopped. As it is, forecasters are predicting that this deluge will break the Seattle record for most rainfall in a single 24-hour period.<\/P>\n<TABLE id=\"EDD\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P><B>Note<\/B>. What? No, the record isn\u2019t 147 feet; surprisingly enough, it\u2019s just a little over 5 inches. The truth is, it usually doesn\u2019t rain <I>hard<\/I> in Seattle; it\u2019s just that the misty-style rain that we <I>do<\/I> get never seems to stop. Seattle, despite its reputation, only gets an average of 37 inches of rain per year; that\u2019s less rain per year than Washington, DC, less rain per year that Miami, FL, less rain per year than New York City, less rain per year than Houston, TX. In other words, less rain per year than lots of places.<\/P>\n<P>Not that any of that is much consolation at the moment, mind you.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>On the bright side, at least there\u2019s not much incentive to go outside and do something, not today anyway. And seeing as how we <I>are<\/I> stuck inside, well, we might try our hand at writing a script that can cut a row from one Microsoft Excel spreadsheet and then paste that row into a second spreadsheet.<\/P>\n<P>You know, something along the lines of <I>this<\/I> script:<\/P><PRE class=\"codeSample\">Const xlDown = -4121<\/p>\n<p>&#8216;Open first spreadsheet\nSet objOldExcel = CreateObject(&#8220;Excel.Application&#8221;)\nobjOldExcel.Visible = True<\/p>\n<p>Set objOldWorkbook = objOldExcel.Workbooks.Open(&#8220;C:\\Scripts\\OldTest.xls&#8221;)\nSet objOldWorksheet = objOldWorkbook.Worksheets(&#8220;Sheet1&#8221;)<\/p>\n<p>&#8216;Open second spreadsheet\nSet objNewExcel = CreateObject(&#8220;Excel.Application&#8221;)\nobjNewExcel.Visible = True<\/p>\n<p>Set objNewWorkbook = objNewExcel.Workbooks.Open(&#8220;C:\\Scripts\\NewTest.xls&#8221;)\nSet objNewWorksheet = objNewWorkbook.Worksheets(&#8220;Sheet1&#8221;)<\/p>\n<p>&#8216;Cut row from the original spreadsheet\nSet objOldRange = objOldWorksheet.Range(&#8220;A1&#8221;).EntireRow\nobjOldRange.Cut<\/p>\n<p>&#8216;Find the first unused row in the second spreadsheet<\/p>\n<p>Set objNewRange = objNewExcel.Range(&#8220;A1&#8221;)\nobjNewRange.End(xlDown).Activate\nintNewRow = objNewExcel.ActiveCell.Row + 1\nstrNewCell = &#8220;A&#8221; &amp;  intNewRow\nobjNewExcel.Range(strNewCell).Activate<\/p>\n<p>&#8216;Paste the data into the second spreadsheet\nobjNewWorksheet.Paste<\/p>\n<p>&#8216;Delete the row from the original spreadsheet\nobjOldRange.Delete\n<\/PRE>\n<P>OK, we admit it: this script is a little more complicated than it probably should be. But don\u2019t worry; we\u2019ll see what we can do to help you understand what\u2019s going on. And because the biggest problem here lies in remembering which of the two spreadsheets you\u2019re working with, we\u2019ve even done something very radical and out-of-character for <I>Hey, Scripting Guy!<\/I>: we\u2019ve added a few comments to the code, hopefully helping you to distinguish between the different parts of the script.<\/P>\n<TABLE id=\"EFE\" 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>. What\u2019s that? Why didn\u2019t we just color code the script? Boy, you guys haven\u2019t done much work with the Web page templates we\u2019re allowed to use, have you? We\u2019ve been trying for more than 3 years just to get <I>boldfacing<\/I> into our code samples. Color coding is something we don\u2019t even bother dreaming about.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>To begin with, we define a constant named xlDown and assign it the odd value -4121; we\u2019ll use that constant when we set out to find the first unused row in worksheet number 2. We then use this block of code to:<\/P>\n<TABLE class=\"numberedList\" border=\"0\" cellSpacing=\"0\" cellPadding=\"0\">\n<TBODY>\n<TR vAlign=\"top\">\n<TD class=\"listNumber\" noWrap align=\"right\">\n<P>1.<\/P><\/TD>\n<TD>\n<P>Create an instance of the Excel.Application object.<\/P><\/TD><\/TR>\n<TR vAlign=\"top\">\n<TD class=\"listNumber\" noWrap align=\"right\">\n<P>2.<\/P><\/TD>\n<TD>\n<P>Set the Visible property to True<\/P><\/TD><\/TR>\n<TR vAlign=\"top\">\n<TD class=\"listNumber\" noWrap align=\"right\">\n<P>3.<\/P><\/TD>\n<TD>\n<P>Open the file C:\\Scripts\\OldTest.xls and bind to the first worksheet in that file<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>Here\u2019s the block of code in question:<\/P><PRE class=\"codeSample\">Set objOldExcel = CreateObject(&#8220;Excel.Application&#8221;)\nobjOldExcel.Visible = True<\/p>\n<p>Set objOldWorkbook = objOldExcel.Workbooks.Open(&#8220;C:\\Scripts\\OldTest.xls&#8221;)\nSet objOldWorksheet = objOldWorkbook.Worksheets(&#8220;Sheet1&#8221;)<\/PRE>\n<P>OK, that wasn\u2019t too bad, was it? Now we\u2019re going to repeat this process, this time opening a spreadsheet named NewTest.xls and binding to the first worksheet in that file:<\/P><PRE class=\"codeSample\">Set objNewExcel = CreateObject(&#8220;Excel.Application&#8221;)\nobjNewExcel.Visible = True<\/p>\n<p>Set objNewWorkbook = objNewExcel.Workbooks.Open(&#8220;C:\\Scripts\\NewTest.xls&#8221;)\nSet objNewWorksheet = objNewWorkbook.Worksheets(&#8220;Sheet1&#8221;)<\/PRE>\n<P>If you were able to follow that, you know that we now have two instances of Excel up and running on screen; one instance uses the object reference objOldExcel, the other uses the object reference objNewExcel. Got that? Good. Then we\u2019re halfway home.<\/P>\n<P>Our next task is to cut row 1 from OldTest.xls, the spreadsheet that objOldExcel refers to. That\u2019s what we do here:<\/P><PRE class=\"codeSample\">Set objOldRange = objOldWorksheet.Range(&#8220;A1&#8221;).EntireRow\nobjOldRange.Cut\n<\/PRE>\n<P>Come to think of it, that doesn\u2019t look all that bad, either. (Does it?) In the first line, we\u2019re simply creating an instance of Excel\u2019s <B>Range<\/B> object. Because we want this range to encompass row 1 we start the range in cell <B>A1<\/B>, then tack on the <B>EntireRow<\/B> property to make sure that all the cells in that row are included in the range.<\/P>\n<P>And then, in line 2, we call the <B>Cut<\/B> method on that range to cut row 1 from the first spreadsheet.<\/P>\n<P>Well, kind of. As it turns out, the Cut method really functions more like the Copy method: it copies the information to the Clipboard, just like you would expect it to. What it <I>doesn\u2019t<\/I> do, however, is delete that information from the original spreadsheet. In other words, we have row 1 on the Clipboard, just like we want. However, we also still have row 1 in the original spreadsheet, just like we <I>don\u2019t<\/I> want. Could this day get any better? <\/P>\n<P>But, hey, don\u2019t worry: we can\u2019t do much about the weather, but we <I>can<\/I> (and will) show you a way to work around the Cut-doesn\u2019t-actually-cut problem.<\/P>\n<P>That brings us to this block of code:<\/P><PRE class=\"codeSample\">Set objNewRange = objNewExcel.Range(&#8220;A1&#8221;)\nobjNewRange.End(xlDown).Activate\nintNewRow = objNewExcel.ActiveCell.Row + 1\nstrNewCell = &#8220;A&#8221; &amp;  intNewRow\nobjNewExcel.Range(strNewCell).Activate\n<\/PRE>\n<P>This is the block of code that enables us to locate the first unused row in our second spreadsheet. (Note that this approach is predicated on the idea that the first blank row in column A is also the first unused row in the spreadsheet.) To locate the first unused row (which will also be the location where we want to paste the row we just cut), we start out by creating a Range object that consists solely of cell A1:<\/P><PRE class=\"codeSample\">Set objNewRange = objExcel.Range(&#8220;A1&#8221;)\n<\/PRE>\n<P>Why do we do that? Well, to find the first unused row we\u2019re going to mimic the behavior of <B>Ctrl+Down Arrow<\/B>; if you press Ctrl+Down Arrow in Excel the cursor will pop down to the last cell in the column that contains data. Before we can do that, however, we need to have the cursor in cell A1; this is one simple way to do that.<\/P>\n<P>Our next step is to actually go ahead and mimic the behavior of <B>Ctrl+Down Arrow<\/B>, something that requires just one line of code:<\/P><PRE class=\"codeSample\">objNewRange.End(xlDown).Activate\n<\/PRE>\n<P>Here we\u2019re using both the <B>End<\/B> property and the <B>Activate<\/B> method to move to the end of the column (that is, the last cell in the column that contains data). To move the cursor to the end of the column we set the value of End to the constant xlDown; that tells Excel that we want to go to the \u201cend\u201d of the column. (Technically, of course, we\u2019re just moving down the column until we encounter a blank cell.) After making that move we then call the Activate method to make that cell (whatever it is) the active cell. <\/P>\n<P>That\u2019s great, except for one thing: we\u2019re still not in the first unused row in the spreadsheet. In fact, the first unused row is the row directly beneath us. How do we get the cursor down one more row?<\/P>\n<P>To tell you the truth there are a couple different ways we could do this. For better or worse we chose this method:<\/P><PRE class=\"codeSample\">intNewRow = objNewExcel.ActiveCell.Row + 1\nstrNewCell = &#8220;A&#8221; &amp;  intNewRow\nobjNewExcel.Range(strNewCell).Activate\n<\/PRE>\n<P>Here we\u2019re assigning a value to a variable named intNewRow; the value we assign will be the row number of the active cell plus 1. Why \u201cplus 1?\u201d Because we don\u2019t want to be in the current row, we want to be down in the <I>next<\/I> row.<\/P>\n<P>We then assign a value to another new variable, strNewCell; this variable gets assigned the letter <B>A<\/B> plus the value of intNewRow. In other words, if the first unused cell in column A is cell A6 then strNewCell gets assigned the value <B>A6<\/B>. To move the cursor to this new cell we create a range consisting of this one cell and call the Activate method. Our cursor is now in a blank cell, we know the address of this cell (A6), and we\u2019re ready to paste in some data.<\/P>\n<P>Which, fortunately, we can do with just a single line of code:<\/P><PRE class=\"codeSample\">objNewWorksheet.Paste\n<\/PRE>\n<P>Now we just have one problem left: the data we pasted into the new spreadsheet is still sitting there in the original spreadsheet; as we noted earlier, the data doesn\u2019t actually get <I>cut<\/I> from its original location. But that\u2019s OK. Seeing as how we already have a Range object that refers to this data all we have to do is call the <B>Delete<\/B> method and the data <I>will<\/I> get deleted from the original spreadsheet:<\/P><PRE class=\"codeSample\">objOldRange.Delete\n<\/PRE>\n<P>Yes, it\u2019s a little weird and a little complicated. But it works. And if you try running the script a few times you\u2019ll soon begin to catch on to <I>how<\/I> it works.<\/P>\n<P>We hope that helps, NC. We also hope that none of you are worrying about the Scripting Guys. Yes, it\u2019s still raining here, and yes there are reports of floods throughout the Seattle area. But remember, the Scripting Guys work for Microsoft: we\u2019re perfectly safe sitting up in our ivory towers, detached and isolated from the real world.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I cut a row from an Office Excel spreadsheet and paste it into the first unused row in another Excel spreadsheet?&#8212; NC Hey, NC. We apologize if today\u2019s column feels a little damp; as it turns out, those of us in the Seattle area are experiencing another one of the [&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,3,5],"class_list":["post-63463","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-excel-spreadsheet","tag-excel-application","tag-microsoft-excel","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I cut a row from an Office Excel spreadsheet and paste it into the first unused row in another Excel spreadsheet?&#8212; NC Hey, NC. We apologize if today\u2019s column feels a little damp; as it turns out, those of us in the Seattle area are experiencing another one of the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/63463","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=63463"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/63463\/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=63463"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=63463"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=63463"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}