使用匹配的工作表名称复制数据

Copy Data with Matching Worksheet Name

我有一个工作簿,其中包含工作sheet的“摘要”(其中合并了所有数据,如图所示)、“8”、“9”、“10”。

我想复制“摘要”中的数据,条件是如果 A 列中的单元格包含作品sheet 名称(8、9 或 10),则该单元格的行和 C 到 E 列将已粘贴到具有匹配名称的作品sheet。

粘贴的数据会偏移到第7行,每个数据都会增加space。例如,“摘要”中 A 列第 2 至 6 行的单元格包含“8”,因此 C 至 E 列第 2 至 6 行将被复制并粘贴到 sheet“8”。

Link 到我的宏文件: https://drive.google.com/file/d/18UalCvxIXuP6imVWZsWLRZPghMqogZp8/view?usp=sharing

此代码不会进行偏移和递增:

Sub Copy_Data()
Application.ScreenUpdating = False
Dim i As Long
Dim j As Double
Sheets("Summary").Activate
Dim lastrow As Long
lastrow = Sheets("Summary").Cells(Rows.Count, "A").End(xlUp).Row
Dim Lastrowa As Long
Dim ans As String

For i = 2 To lastrow
    ans = Cells(i, "A").Value
    Lastrowa = Sheets(ans).Cells(Rows.Count, "C").End(xlUp).Row
    Sheets("Summary").Rows(i).Columns("C:E").Copy
    Sheets(ans).Rows(Lastrowa + 1).Columns("C:E").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Next i
Application.ScreenUpdating = True
End Sub
Sub Copy_Data()
    Dim lastRow As Long, offsetRow As Long, i As Long, No As String, NOSheet As Worksheet, auxRow As Long, summarySheet As Worksheet
    Set summarySheet = Worksheets("Summary")
    lastRow = summarySheet.Columns("A").Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
    offsetRow = 7
    For i = 2 To lastRow
        No = Cells(i, "A")
        Set NOSheet = Worksheets(No)
        auxRow = NOSheet.Columns("C").Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
        If auxRow > 1 Then auxRow = auxRow + 2
        If auxRow = 1 Then auxRow = offsetRow
        NOSheet.Cells(auxRow, "C") = summarySheet.Cells(i, "C")
        NOSheet.Cells(auxRow, "D") = summarySheet.Cells(i, "D")
        NOSheet.Cells(auxRow, "E") = summarySheet.Cells(i, "E")
    Next i
End Sub

从一张到多张工作表

  • 假定 Source Worksheet 数据是连续的并且从单元格 A1 (CurrentRegion) 开始。
  • 调整常量部分和工作簿中的值。
Option Explicit

Sub CopyData()
    
    Const sName As String = "Summary" ' Source Worksheet Name
    Const sdCol As String = "A" ' Destination Worksheet Name Column
    Const sCols As String = "C:E" ' Source Copy Columns
    Const sFirst As Long = 2 ' Source First Row
    
    Const dCol As String = "C" ' Destination First (Paste) Column
    Const dFirst As Long = 7 ' Destination First Row
    Const drOffset As Long = 2 ' Destination Row Offset
    
    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    
    Dim sws As Worksheet: Set sws = wb.Worksheets(sName)
    Dim srg As Range: Set srg = sws.Range("A1").CurrentRegion
    
    Dim sLast As Long: sLast = srg.Rows.Count ' Source Last Row
    Dim cCount As Long: cCount = sws.Columns(sCols).Columns.Count
    
    Application.ScreenUpdating = False
    
    Dim srrg As Range ' Source Row Range
    Dim r As Long ' Source Row Counter
    
    Dim dws As Worksheet ' Destination Worksheet
    Dim drrg As Range ' Destination Row Range
    Dim dCell As Range ' Destination Last Cell
    Dim dName As String ' Destination Worksheet Name
    
    For r = sFirst To sLast
        dName = CStr(srg.Columns(sdCol).Rows(r).Value)
        Set dws = Nothing
        On Error Resume Next
        Set dws = wb.Worksheets(dName)
        On Error GoTo 0
        If Not dws Is Nothing Then
            Set dCell = dws.Cells(dws.Rows.Count, dCol).End(xlUp)
            If dCell.Row < dFirst Then
                Set drrg = dws.Cells(dFirst, dCol).Resize(, cCount)
            Else
                Set drrg = dCell.Offset(drOffset).Resize(, cCount)
            End If
            Set srrg = srg.Columns(sCols).Rows(r)
            drrg.Value = srrg.Value
        'Else
            ' Destination Worksheet doesn't exist.
        End If
    Next r
    
    Application.ScreenUpdating = True

End Sub