简体   繁体   English

如何复制行并将它们粘贴到给定单元格值的工作表中

[英]How to copy rows and paste them into a sheet given a cell value

I have data in a table, where I compare two columns J and T. The values which J and T can take include A2B, APL, BGF, CMA, among others (see code).我在表中有数据,我在其中比较了两列 J 和 T。 J 和 T 可以采用的值包括 A2B、APL、BGF、CMA 等(参见代码)。

If these values are equal, copy row i into the sheet which has the name of the cells just checked.如果这些值相等,将第 i 行复制到刚刚选中的单元格名称的工作表中。

If these values are not equal, copy row i into the sheets which have the name of the cells just checked.如果这些值不相等,请将第 i 行复制到刚刚选中单元格名称的工作表中。

Example : Compare J2 and T2,示例:比较 J2 和 T2,

Suppose J2=T2=BGF then copy row 2 and paste into sheet("BGF")假设 J2=T2=BGF 然后复制第 2 行并粘贴到 sheet("BGF")

Next, compare J3 and T3接下来比较J3和T3

Suppose J3=BGF and T3=CMA, copy row 3 and paste into sheet(BGF) and sheet(CMA)假设J3=BGF和T3=CMA,复制第3行并粘贴到sheet(BGF)和sheet(CMA)

Next, compare J4 and T4接下来比较J4和T4

Suppose J4=Nothing and T4=CMA, copy row 4 and paste into sheet CMA假设 J4=Nothing 和 T4=CMA,复制第 4 行并粘贴到工作表 CMA

the only other combination is where Ji has a value and Ti is empty.唯一的其他组合是 Ji 有值而 Ti 为空。

Problem : When running this code, If J3=BGF and T3= nothing (its empty), then the line is not copied to any sheet.问题:运行此代码时,如果 J3=BGF 且 T3= nothing(其为空),则该行不会复制到任何工作表。

Here's the code这是代码

Sub Sortdata()
    'step 1 clear all data
    Sheets("A2B").Cells.ClearContents
    Sheets("APL").Cells.ClearContents
    Sheets("BGF").Cells.ClearContents
    Sheets("CMA").Cells.ClearContents
    Sheets("K Line").Cells.ClearContents
    Sheets("MacAndrews").Cells.ClearContents
    Sheets("Maersk").Cells.ClearContents
    Sheets("OOCL").Cells.ClearContents
    Sheets("OPDR").Cells.ClearContents
    Sheets("Samskip").Cells.ClearContents
    Sheets("Unifeeder").Cells.ClearContents

    Dim i As Long
    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    Dim LastRow As Long


    With Worksheets("All Data")
        LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
        For i = 2 To LastRow
            If IsEmpty(.Range("J" & i)) Then
                Set ws1 = Nothing
            Else
                Set ws1 = Worksheets(.Range("J" & i).Value)
            End If
            If IsEmpty(.Range("T" & i)) Then
                Set ws2 = Nothing
            Else
                Set ws2 = Worksheets(.Range("T" & i).Value)
            End If

            If ws1 Is Nothing Then
                If Not ws2 Is Nothing Then
                    CopyToWs ws2, .Rows(i)
                End If
            ElseIf ws2 Is Nothing Then
                If Not ws1 Is Nothing Then
                    CopyToWs ws1, .Rows(i)
                End If
            Else
                CopyToWs ws1, Rows(i)
                If ws1.Name <> ws2.Name Then
                    CopyToWs ws2, .Rows(i)
                End If
            End If
        Next
    End With

End Sub

Sub CopyToWs(ws As Worksheet, rng As Range)
    rng.Copy
    ws.Cells(ws.Rows.Count, 1).End(xlUp).Offset(1, 0).EntireRow.PasteSpecial xlPasteValuesAndNumberFormats
End Sub

Please try this code.请试试这个代码。 It takes a slightly different approach to what you tried but it gets the job done, I think.它与您尝试的方法略有不同,但我认为它可以完成工作。

Option Explicit

Sub Sortdata()
    ' Variatus @STO 20 Jan 2020

    Const WsNames As String = "A2B,APL,BGF,CMA,K Line,MacAndrews," & _
                              "Maersk,OOCL,OPDR,Samskip,Unifeeder"

    Dim WsS As Worksheet                ' Source
    Dim Ws As Worksheet
    Dim Rng As Range
    Dim Rt As Long                      ' target row
    Dim LastRow As Long
    Dim J As Long, T As Long
    Dim Tmp As Variant, PrevTmp As Variant
    Dim R As Long, C As Long

    'step 1 clear all data
    Tmp = Split(WsNames, ",")
    For R = LBound(Tmp) To UBound(Tmp)
        On Error Resume Next
        Worksheets(Tmp(R)).Cells.ClearContents
    Next R

    Application.ScreenUpdating = False
    Set WsS = Worksheets("All Data")
    With WsS
        J = .Columns("J").Column
        T = .Columns("T").Column
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        For R = 2 To LastRow
            PrevTmp = ""
            For C = J To T Step T - J
                Tmp = .Cells(R, C).Value
                If Len(Tmp) And Tmp <> PrevTmp Then
                    On Error Resume Next
                    Set Ws = Worksheets(Tmp)
                    If Err = 0 Then
                        Set Rng = .Range(.Cells(R, 1), .Cells(R, .Columns.Count).End(xlToLeft))
                        With Ws
                            Rt = Application.Max(.Cells(.Rows.Count, "A").End(xlUp).Row + 1, 2)
                            Rng.Copy Destination:=Ws.Cells(Rt, 1)
                        End With
                    End If
                End If
                PrevTmp = Tmp
            Next C
            If R Mod 25 = 0 Then Application.StatusBar = "Currently processing row " & R
        Next R
    End With

    With Application
        .ScreenUpdating = True
        .StatusBar = "Done"
    End With
End Sub

I think you will be able to find your way through it and make any required modifications.我认为您将能够找到自己的方式并进行任何必要的修改。 Let me know if you require any assistance.如果您需要任何帮助,请告诉我。

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

相关问题 根据单元格值复制行并粘贴到新工作表上 - Copy rows based on cell value and paste on a new sheet 如何从另一个工作表中的特定单元格开始复制和粘贴行 - How to Copy and Paste Rows starting from a specific cell in another sheet 根据单元格值复制行并粘贴到具有相同单元格值名称的新工作表上 - Copy rows based on cell value and paste on a new sheet with same cell value name 基于一个单元格值复制行并引用另一个单元格值并粘贴到新工作表上 - Copy rows based on one cell value and in reference to another cell value and paste on a new sheet 如何复制其他工作表中的行并将其粘贴到其中有表格的工作表中? - How do i copy rows from another sheet and paste them into a sheet that has a table in it? 复制并粘贴 excel 表,要在代码中引用的表单元格数据的值 - Copy and Paste a excel sheet , Value of Sheet Cell Data to be referenced in code 根据单元格值复制和粘贴行 - Copy and Paste Rows based on cell value 根据单元格值(日期)复制和粘贴行 - Copy and paste rows based on cell value (date) 如何在不同的单元格范围内将具有不同值的整行复制并粘贴到新表中? - How can I copy & paste entire rows with distinct values to a new sheet on varying cell ranges? 根据单元格值将行复制到工作簿表中 - Copy rows into a Workbook sheet based on cell value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM