简体   繁体   English

excel 2007从多个工作表复制相同的范围并粘贴到当前工作表

[英]excel 2007 Copy the same range from multiple sheets and paste to current sheet

New to vba, I looked everywhere, tried a few posted macros and modified them for me but nothing ever works perfect and I'm getting frustrated, Help. vba的新手,我到处都看过,尝试了一些发布的宏并为我修改了它们,但没有什么能完美完成,我对此感到沮丧。 I need to copy a range of values from multiple Sheets and paste those ranges in one sheet. 我需要从多个工作表中复制一定范围的值,并将这些范围粘贴到一张工作表中。 The first sheet (Totals) will have the macro (as a button). 第一张纸(总计)将具有宏(作为按钮)。 Each sheet after sheet 1, represents an employee so the name of each sheet is their name. 在工作表1之后的每个工作表都代表一个雇员,因此每个工作表的名称就是他们的名字。 The number of sheets will vary from time to time as employees come and go. 随着员工的来回,工作表的数量会不时变化。 The range I need to copy is the same for all the employees, (N5:V400). 我需要复制的范围对于所有员工都是相同的(N5:V400)。 Although the number of rows which contain values in that range will vary from employee to employee. 尽管包含该值的行的数量将因员工而异。 I also need to copy the employees last name in each sheet(cell Q2) and paste it as the leading cell for each row that gets pasted into the Totals Sheet. 我还需要在每个工作表(单元格Q2)中复制员工的姓氏,并将其粘贴为总计表中每一行的开头单元格。

So when the Macro runs, Cell Q2(Name) from each employee sheet and any values in range N5:V500 get pasted into the Totals sheet Range A3:J5000 (there are two header rows). 因此,当宏运行时,每个员工表中的单元格Q2(Name)和范围N5:V500中的任何值都将粘贴到总计表范围A3:J5000中(有两个标题行)。 The name needs to the first cell of every row that gets pasted for each employee. 该名称需要粘贴到每个员工的每一行的第一个单元格中。

I'm sure this is a simple code, but then again I am certainly not qualified to say. 我确定这是一个简单的代码,但是我当然再没有资格说。 Thanks to all who can help. 感谢所有能提供帮助的人。
在此处输入图片说明

Here, try this: 在这里,尝试一下:

Tried and Tested: 经过测试:

Option Explicit
Sub test()

Dim ws As Worksheet, wsTotals As Worksheet
Dim lrow As Long

Set wsTotals = ThisWorkbook.Sheets("Totals")

For Each ws In ThisWorkbook.Worksheets
    If ws.Name <> "Totals" Then
        lrow = ws.Range("N" & Rows.Count).End(xlUp).Row
        If lrow > 4 Then
            ws.Range("N5:V" & lrow).Copy wsTotals.Range("B" & Rows.Count).End(xlUp).Offset(1, 0)
            ws.Range("Q2").Copy wsTotals.Range(wsTotals.Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Address, _
                wsTotals.Range("A" & Rows.Count).End(xlUp).Offset(lrow - 4, 0).Address)
        End If
    End If
Next ws

End Sub

This should do it. 这应该做。
Hope this works for you. 希望这对您有用。
It's workning but, haven't have time to do a lot of test. 它正在工作,但是没有时间做很多测试。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Excel 2007复制范围值并粘贴到另一个范围(同一张纸)的第一个空行 - Excel 2007 Copy range values and paste to another range (Same Sheet) first empty row Excel VBA-将范围从一个工作表粘贴复制到工作簿中的某些工作表之后的所有工作表 - Excel VBA - Copy range from one sheet paste to all sheets after certain sheet in workbook 将多张纸中的同一行复制到Excel中的一张纸中 - copy the same row from multiple sheets into one sheet in excel 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? Excel VBA-复制/粘贴范围从一张纸到所有后续纸 - Excel VBA - Copy/Paste range from one sheet to all proceeding sheets 将多张纸(但不是全部纸)的值复制/粘贴到一张纸中 - Copy/paste values from multiple sheets, but not all sheets, into one sheet VBA复制来自不同范围的粘贴值,然后粘贴到同一张纸,同一行偏移列上(重复多张纸) - VBA Copy Paste Values From Separate Ranges And Paste On Same Sheet, Same Row Offset Columns (Repeat For Multiple Sheets) Excel复制/粘贴在同一张纸上 - Excel copy/paste on same sheet 从多个Excel文件复制(动态范围)并粘贴到主表中(动态范围) - Copy from Multiple Excel Files (dynamic ranges) and Paste in master Sheet (Dynamic range) 从单元格J中包含“是”的Sheet1复制行并将其粘贴到Excel 2007中的Sheet4 - Copy and Paste a Row from Sheet1 Containing “YES” in Cell J to Sheet4 in Excel 2007
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM