[英]VBA Code to Copy Data from Row 2 to Last Row of Data on One Sheet and Paste to the First Empty Row of Another Sheet
I am working with multiple sheets within a workbook.我正在处理一个工作簿中的多个工作表。 Each sheet has an identical header row.
每个工作表都有一个相同的标题行。 I would like to write a macro to copy a range of data from each sheet (A2:L2 to the last row of data on a sheet) and paste it into the first empty cell in Column A of master sheet.
我想编写一个宏来复制每个工作表中的一系列数据(A2:L2 到工作表上的最后一行数据)并将其粘贴到主表 A 列的第一个空单元格中。
What I have below doesn't seem to work.我下面的内容似乎不起作用。 Any assistance is appreciated.
任何帮助表示赞赏。
Dim Lastrow As Integer
Lastrow = ActiveSheet.Cells(Rows.Count,1).End(xlUp).Row
Sheets("Master Sheet").Range("A2:L10000).Clear
Sheets("Sheet1").Activate
Range("A2:L" & Lastrow).Select
Selection.Copy
Sheets("Master Sheet").Select
Range("A30000").Select
Selection.End(xlUp).Select
ActiveCell.Offset(1,0).Range("A1").Select
ActiveSheet.Paste
Sheets("Sheet2").Activate
Range("A2:L" & Lastrow).Select
Selection.Copy
Sheets("Master Sheet").Select
Range("A30000").Select
Selection.End(xlUp).Select
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveSheet.Paste
You're almost there from the looks of it.从它的外观来看,您几乎就在那里。 It looks like you've recorded a macro which is a good place to start.
看起来您已经录制了一个宏,这是一个很好的起点。 The reason your specific code might not work is because of
Sheets("Master Sheet").Range("A2:L10000).Clear
. You're missing the end quote inside of the range. In any case though, I've chosen to leave that out so you don't accidentally clear your sheet when you're moving data across您的特定代码可能不起作用的原因是因为
Sheets("Master Sheet").Range("A2:L10000).Clear
。你错过了范围内的结束引号。无论如何,我选择了将其排除在外,这样您在移动数据时就不会意外清除工作表
So generally it's good to avoid using select
and activate
, but with a recorder you don't have much say in the matter.所以通常避免使用
select
和activate
是好的,但是对于记录器,您在这件事上没有太多发言权。 Below you can see how I can do operations directly with the range.您可以在下面看到我如何直接对范围进行操作。
It's important to find the last row of the master sheet as well as your current sheet each time so you know the range you want to copy and where you can paste it.每次找到主表的最后一行以及当前表很重要,这样您就知道要复制的范围以及可以粘贴到的位置。 It's important to remember that you're always finding the last filled cell, so in the case of a paste destination, you need to add one more to the row value so you don't accidentally overwrite some data.
重要的是要记住,您总是会找到最后一个填充的单元格,因此在粘贴目标的情况下,您需要在行值中再添加一个,以免意外覆盖某些数据。
For loops are pretty useful, I'm not sure what names you have for the rest of your sheets, but luckily in VBA you can use For each
. For 循环非常有用,我不确定您对其余工作表的名称,但幸运的是在 VBA 中您可以使用
For each
。 so what this does is it cycles through every single item that you specify.所以它的作用是循环遍历您指定的每个项目。 In this case I've specified worksheets.
在这种情况下,我指定了工作表。 The only problem now though, is we don't want to try copying and pasting into the same Master Sheet, so we need to do a quick check to make sure we're not on the master sheet.
但现在唯一的问题是,我们不想尝试复制和粘贴到同一个主表中,因此我们需要快速检查以确保我们不在主表上。 I've done this simply by comparing the name of the worksheet to what you've put as the name of the master sheet.
我只是通过将工作表的名称与您作为主表名称的名称进行比较来完成此操作的。
Interestingly when you copy something, you only need to specify the top left cell and it'll fill in the rest of it.有趣的是,当你复制一些东西时,你只需要指定左上角的单元格,它就会填充其余的单元格。 It makes life a little easier because you don't need to figure out the exact dimensions of the array you're pasting.
它让生活变得更轻松,因为您不需要弄清楚要粘贴的数组的确切尺寸。 The
Copy
function in VBA has an optional parameter called Destination
which you can use to tell it where you want to paste it right away. VBA 中的
Copy
函数有一个名为Destination
的可选参数,您可以使用它来告诉它要立即将其粘贴到何处。
It's also good to fully specify ranges when you're using them so instead of Range
, you can see how I use ThisWorkbook.Worksheets("Master Sheet").Range
.这也是不错的,当你使用它们所以不是完全指定范围的
Range
,你可以看到我如何使用ThisWorkbook.Worksheets("Master Sheet").Range
。 This tells the computer exactly where you want to reference;这会告诉计算机您要引用的确切位置; whereas
Range
makes the computer sort of guess, so it assumes you mean the active sheet, which might not be what you want.而
Range
使计算机进行某种猜测,因此它假定您指的是活动工作表,这可能不是您想要的。
Sub Paster()
Dim LastRowCurr As Long
Dim LastRowMaster As Long
Dim wksht As Worksheet
For Each wksht In ThisWorkbook.Worksheets
If Not wksht.Name = "Master Sheet" Then
LastRowCurr = wksht.Cells(wksht.Rows.Count, 1).End(xlUp).Row
LastRowMaster = Worksheets("Master Sheet").Cells(Worksheets("Master Sheet").Rows.Count, 1).End(xlUp).Row + 1
Range("A2:L" & LastRowCurr).Copy Destination:=ThisWorkbook.Worksheets("Master Sheet").Cells(LastRowMaster, "A")
End If
Next wksht
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.