[英]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.