简体   繁体   English

用于将数据从第 2 行复制到一张工作表上的最后一行数据并粘贴到另一张工作表的第一空行的 VBA 代码

[英]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.所以通常避免使用selectactivate是好的,但是对于记录器,您在这件事上没有太多发言权。 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.

相关问题 复制列直到一张纸的最后一行,然后粘贴到另一张纸的下一个空行 - Copy columns until last row from one sheet and paste to the next empty row of another sheet 从第二行复制一列数据,直到其中有数据的最后一行,然后粘贴为另一张表 VBA 上一列的最后一行 - Copy a column of data from the second row until the last row with data in it and paste as the last row in a column on another sheet VBA 将数据从一张纸复制到另一张纸的最后一行 - Copy data from one sheet to the last row of another sheet VBA:识别工作表中的最后一行公式(或数据),复制该行,然后在其正下方粘贴一行 - VBA: Identify Last row of formulas (or data) in sheet, copy that row, and then paste one row right below it Excel 2007 VBA:如何从一张纸上的动态范围复制和粘贴到另一张纸的第一行? - Excel 2007 VBA: How do I copy and paste from a dynamic range on one sheet to the first empty row of another sheet? 使用VBA excel从一张工作表中复制表格并将其粘贴到另一张工作表的下一个空行中 - copy a table from one Sheet and paste it in the next empty row of another Sheet using VBA excel 我正在编写 vba 代码以一次从一张纸复制一行数据并粘贴到另一张纸中 - I am writing vba code to copy one row of data at a time from one sheet and pasting into another sheet 使用宏从一个工作表中复制多个范围,然后将其粘贴到第一行中的另一工作表中 - Using macro to copy multiple ranges from one sheet, and paste them into another sheet in the first empty row Excel VBA 复制一张表的第一行数据并粘贴到另一张表 - Excel VBA to copy 1st row of data of one sheet and paste to another sheet VBA从自动筛选器中复制并粘贴到另一张工作表中,输出一行 - VBA Copy and Paste in another Sheet from AutoFilter outputting one row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM