[英]I need to copy a specific range in multiple sheets and paste them on a final sheet
There are 24 sheets in this workbook
.本
workbook
共有 24 张工作表。 I need to copy the same range from 23 sheets
and paste them in a final sheet called "ALL SURVEY".我需要从 23
sheets
中复制相同的范围并将它们粘贴到名为“ALL SURVEY”的最后一张纸中。 Is there any way to code it in such a way that I don't need to write so much code as I did in the following macro?有没有什么方法可以让我不需要像在下面的宏中那样编写那么多代码?
Sheets("2").Range("U3:X3").Copy
Sheets("ALL SURVEY").Range("E2").*PasteSpecial xlPasteValues*
Sheets("3").Range("U3:X3").Copy
Sheets("ALL SURVEY").Range("E3").*PasteSpecial xlPasteValues*
Sheets("4").Range("U3:X3").Copy
Sheets("ALL SURVEY").Range("E4").*PasteSpecial xlPasteValues*
Sheets("5").Range("U3:X3").Copy
Sheets("ALL SURVEY").Range("E5").*PasteSpecial xlPasteValues*
It will be much appreciated if you help me get through this hard task Thank you如果您能帮助我完成这项艰巨的任务,将不胜感激谢谢
You can use a For...Next
loop for this:您可以为此使用
For...Next
循环:
Sub Tester()
Dim n As Long, c As Range
Set c = ThisWorkbook.Sheets("ALL SURVEY").Range("E2") 'first destination cell
'loop through sheets
For n = 2 To 23
'convert n to string to get the correct sheet
' Sheets("2") vs Sheets(2) - by sheet Name vs. Index
With ThisWorkbook.Sheets(CStr(n)).Range("U3:X3")
c.Resize(.Rows.Count, .Columns.Count).Value = .Value 'set values
Set c = c.Offset(1, 0) 'next destination
End With
Next n
End Sub
You can do something like this:你可以这样做:
Sub copyPaste()
Dim survey_sheet As Worksheet, count As Long
count = 1 'start pasting from this row
For Each survey_sheet In ThisWorkbook.Sheets
If survey_sheet.Name <> "ALL SURVEY" Then
survey_sheet.Range("U3:X3").Copy
Sheets("ALL SURVEY").Range("E" & count).PasteSpecial xlPasteValues
count = count + 1
End If
Next survey_sheet
End Sub
As you can see in the macro above, there is a loop For
all the sheets in the Workbook
.正如您在上面的宏中看到的,
For
Workbook
中的所有工作表都有一个循环。 It will end when it has gone through every single one.当它经历了每一个时,它就会结束。
The If
statement is to avoid copy/pasting in the final sheet ant the count
variable is for pasting in the next empty row on "ALL SURVEY" sheet. If
语句是为了避免在最终工作表 ant 中复制/粘贴, count
变量用于粘贴到“ALL SURVEY”工作表的下一个空行中。
Exceptions List
.Exceptions List
。 I added those two 'funny' names just to show that you have to separate them by the Delimiter
with no spaces.Delimiter
将它们分开,没有空格。 The list can contain non-existing worksheet names, but it won't help, so remove them and add others if necessary.U3:X5
, Z7:AS13
).U3:X5
、 Z7:AS13
)。 The result will be each next range below the other (by rows).Exceptions List
and will write the values of the given range to 2D one-based arrays in an Array List
.Exceptions List
中的所有工作表,并将给定范围的值写入Array List
中的二维基于一的 arrays 。 Then it will loop through the arrays of the Array List
and copy the values to the resulting Data Array
whose values will then be copied to the Destination Range
.Array List
的 arrays 并将值复制到生成的Data Array
,然后将其值复制到Destination Range
。 The Code编码
Option Explicit
Sub copyByRows()
Const dstName As String = "ALL SURVEY"
Const dstFirst As String = "E2"
Const srcRange As String = "U3:X3"
Const Delimiter As String = ","
Dim ExceptionsList As String
ExceptionsList = dstName & Delimiter & "Sheet500,Sheet1000"
Dim wb As Workbook: Set wb = ThisWorkbook
Dim dst As Worksheet: Set dst = wb.Worksheets(dstName)
Dim srCount As Long: srCount = dst.Range(srcRange).Rows.Count
Dim cCount As Long: cCount = dst.Range(srcRange).Columns.Count
Dim arl As Object: Set arl = CreateObject("System.Collections.ArrayList")
Dim Exceptions() As String: Exceptions = Split(ExceptionsList, Delimiter)
Dim ws As Worksheet
For Each ws In wb.Worksheets
If IsError(Application.Match(ws.Name, Exceptions, 0)) Then
arl.Add ws.Range(srcRange).Value
End If
Next ws
Dim Data As Variant: ReDim Data(1 To arl.Count * srCount, 1 To cCount)
Dim Item As Variant
Dim i As Long
Dim j As Long
Dim k As Long
For Each Item In arl
For i = 1 To srCount
k = k + 1
For j = 1 To cCount
Data(k, j) = Item(i, j)
Next j
Next i
Next Item
dst.Range(dstFirst).Resize(k, cCount).Value = Data
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.