简体   繁体   English

我需要在多张纸上复制特定范围并将它们粘贴到最后一张纸上

[英]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”工作表的下一个空行中。

Copy Ranges by Rows按行复制范围

  • Adjust the values in the constants section.调整常量部分中的值。 Pay attention to the 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.该列表可以包含不存在的工作表名称,但这无济于事,因此请删除它们并在必要时添加其他名称。
  • You can resize the 'copy' range as you desire (eg U3:X5 , Z7:AS13 ).您可以根据需要调整“复制”范围的大小(例如U3:X5Z7:AS13 )。 The result will be each next range below the other (by rows).结果将是彼此下方的每个下一个范围(按行)。
  • Basically, the code will loop through all worksheets whose names are not in the 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.

相关问题 将多张工作表中的 1 列复制到同一工作簿中的一张工作表中,然后从同一张最终工作表中复制/粘贴第二列 - Copying 1 column from multiple sheets into one sheet in the same workbook and them copy/paste a 2nd column from the same final sheet 如何复制一系列公式值并将它们粘贴到另一个工作表中的特定范围? - How do I copy a range of formula values and paste them to a specific range in another sheet? 如何将特定范围单元格从多张纸复制到一张纸? - How to copy specific range cells from multiple sheets to one sheet? 添加多个工作表,命名它们,并将粘贴动态范围复制到新工作表中 - Add multiple sheets, name them, and copy paste dynamic range into new sheets excel 2007从多个工作表复制相同的范围并粘贴到当前工作表 - excel 2007 Copy the same range from multiple sheets and paste to current sheet 最后一列、行和范围从多张纸复制粘贴到一张纸 - Last Col, Row and Range Copy Paste from multiple sheets to one sheet 将多张纸(但不是全部纸)的值复制/粘贴到一张纸中 - Copy/paste values from multiple sheets, but not all sheets, into one sheet VBA 从多个工作表中复制特定行,其名称包含“Hawk”并粘贴到新工作表中 - VBA Copy specific rows from multiple sheets with their names containing “Hawk” and paste into new sheet 需要从几张不同的纸上垂直复制并粘贴到一张纸上 - Need to copy & paste from several different sheets into one sheet vertically 我想复制一系列单元格并将它们粘贴到另一个工作表,具体取决于下拉选择并使用按钮激活 - I want to copy a range of cells and paste them to another sheet dependent on a drop down selection and activated using a button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM