简体   繁体   English

如果C的单元格值与工作表名称匹配,如何将行复制到另一工作表

[英]How to copy row to another sheet if the cell value of C matches the sheetname

So I pull data then I have to copy and paste the rows to their respective sheets basing on the value of Column D. I have a code that does the thing but it takes too slow when there are thousands of rows. 因此,我提取数据,然后必须根据D列的值将行复制并粘贴到各自的工作表中。我有一个代码可以执行此操作,但是当成千上万行时,它的处理速度太慢。

Sub COPY_DATA()

    Dim bottomD As Long
    bottomD = Range("D" & Rows.Count).End(xlUp).Row
    Dim c As Range
    Dim ws As Worksheet
    For Each c In Sheets("Data").Range("D2:D" & bottomD)
        For Each ws In Sheets
            ws.Activate
            If ws.Name = c And ws.Name <> "Userform" Then
                c.EntireRow.copy Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
            End If
        Next ws
    Next c

Worksheets("Data").Activate

End Sub

I want to make the process of copy and pasting faster 我想使复制和粘贴过程更快

This should be faster: 这应该更快:

Sub COPY_DATA()

    Dim dict As Object
    Dim bottomD As Long
    Dim c As Range
    Dim ws As Worksheet,wb as workbook, wsData as Worksheet

    Set wb = ActiveWorkbook
    Set wsData = wb.worksheets("Data")

    'collect the sheet names
    Set dict = CreateObject("scripting.dictionary")
    For Each ws In wb.Worksheets
        If ws.Name <> "Userform" Then dict.Add ws.Name, True
    Next ws

    Application.ScreenUpdating = False

    bottomD = wsData.Range("D" & Rows.Count).End(xlUp).Row
    For Each c In wsData.Range("D2:D" & bottomD)
        If dict.exists(c.Value) Then
            c.EntireRow.Copy wb.Worksheets(c.Value).Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
        End If
    Next c

    Application.ScreenUpdating = True

    wsData.Activate

End Sub

Stop .Activating ! 停止。 .Activating Totally unnecessary and updating the UI is taking time. 完全不必要,更新UI需要花费时间。 Make sure all calls to ranges are qualified. 确保对范围的所有调用均合格。

Option Explicit '<--- Always at the top of modules!
Sub COPY_DATA()

    Dim bottomD As Long
    bottomD = Range("D" & Rows.Count).End(xlUp).Row
    Dim c As Range
    Dim ws As Worksheet
    For Each c In Sheets("Data").Range("D2:D" & bottomD)
        For Each ws In Sheets
            With ws
                If .Name = c.Value And .Name <> "Userform" Then
                    c.EntireRow.copy Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0)
                End If
            End With
        Next ws
    Next c
End Sub

Note also that I explicitly stated c.Value instead of using the implicit/default property (which just happens to be Value). 还要注意,我明确声明了c.Value而不是使用隐式/默认属性(恰好是Value)。

暂无
暂无

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

相关问题 根据单元格中的值将行复制到另一张工作表 - Copy row to another sheet based on value in a cell VBA如果单元格与整个工作表的值匹配,则复制整个行 - VBA Copy entire row if cell matches a value for entire sheet 将行复制到不同的工作表,如果单元格值与另一个工作表匹配 - Copy Rows to different sheets, IF cell value matches to another sheet 根据单元格值将特定数据(不是整行!)复制到另一张表 - Copy Specific data (Not the entire row!) to another sheet based on cell value Excel如果单元格&lt;=值,如何将数据行从工作表1复制到工作表2 - Excel how copy row of data from sheet 1 to sheet 2 if a cell <= Value 如果单元格与另一个单元格匹配,则复制值 - If Cell Matches another Cell, Copy Value 如何根据单元格值将行内的范围从一个 Excel 工作表复制到另一个 - How to copy a range within a row from one excel sheet to another based on a cell value 如果单元格为true,如何将整行复制到另一张工作表 - How to copy an entire row to another sheet if a cell = true 如何使用单元格中写入的工作表名称引用工作表中的单元格 - How to reference a cell in sheet using the sheetname written in cell 如果工作表sheet2上的单元格与工作表sheet1上的单元格匹配,则将行从工作表2复制到工作表1并循环到下一行 - If cell on sheet2 row1 matches cell on sheet1 then copy row from sheet 2 to sheet 1 and loop for next row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM