简体   繁体   中英

Copy and Paste to a different worksheet and column

I am trying to copy and paste rows A:E if the correct status and name are selected. For example, if the status shows, "In Progress" in column A or name "Thomas Xiong", in column B. Then it will copy and paste the row to a different worksheet called, "WIPTX". In worksheet "WIPTX" it will then copy and paste that row under the next available row under Columns I:M. The following columns are as follows for worksheet WIPTX.

Status   Columns

Assigned A:E
Accepted F:J
In Progress K:O
On Hold P:T
Completed U:Y
Cancelled Z:AD

I have been researching for quite sometime now and have tested many codes to see if it will work. The last code I tried gave me a syntax error after case "In Progress".

Sub Hello()
Dim lRow As Long, cRow As Long, j As Long

With Sheets("WIPdata")
    lRow = .Range("A800").End(xlUp).Row

    ' another method of finding last row in Column A (skipping blank cells in the middle)
    lRow = .Cells(.Rows.Count, "A").End(xlUp).Row

    For j = lRow To 1 Step -1
        cRow = Sheets("WIPTX").Range("A800").End(xlUp).Row

        Select Case .Range("J" & j).Value
            Case "Assigned"
                .Range("A" & j & ":E" & j).Copy Destination:=Sheets("WIPTX").Cells(Sheets("WIPTX").Cells(Sheets("WIPTX").Rows.Count, "A").End(xlUp).Row + 1, "A")

            Case "Accepted"
                .Range("A" & j & ":E" & j).Copy Destination:=Sheets("WIPTX").Cells(Sheets("WIPTX").Cells(Sheets("WIPTX").Rows.Count, "E").End(xlUp).Row + 1, "E")

            Case "In Progress"
                .Range("A" & j & ":E" & j).Copy Destination:=Sheets("WIPTX").Range("A" & cRow + 1).Cells(Sheets("WIPTX").Rows.Count, "I").End(xlUp).Row + 1, "I")

            Case "On Hold"
                .Range("A" & j & ":E" & j).Copy Destination:=Sheets("WIPTX").Range("A" & cRow + 1).Cells(Sheets("WIPTX").Rows.Count, "M").End(xlUp).Row + 1, "M")

            Case "Completed"
                .Range("A" & j & ":E" & j).Copy Destination:=Sheets("WIPTX").Range("A" & cRow + 1).Cells(Sheets("WIPTX").Rows.Count, "Q").End(xlUp).Row + 1, "Q")

            Case "Cancelled"
                .Range("A" & j & ":E" & j).Copy Destination:=Sheets("WIPTX").Range("A" & cRow + 1).Cells(Sheets("WIPTX").Rows.Count, "U").End(xlUp).Row + 1, "U")
        End Select
    Next
End With



End Sub
Sub InProgress()

Dim idxprogress As Integer
idxprogress = 3

Dim idxassigned As Integer
idxassigned = 3

Dim idxonhold As Integer
idxonhold = 3

Dim idxcancelled As Integer
idxcancelled = 3

Dim idxaccepted As Integer
idxaccepted = 3

Dim idxcompleted As Integer
idxcompleted = 3


Sheets("WIPdata").Activate
For Each cell In Range("A2:A55")
Sheets("WIPdata").Activate
Select Case cell.Value

Case "In Progress"
Sheets("WIPdata").Range("B2:F2").Copy
Sheets("WIPTX").Activate
Sheets("WIPTX").Range(Cells(idxprogress, 11), Cells(idxprogress, 15)).PasteSpecial
idxprogress = idxprogress + 1

Case "Assigned"
Sheets("WIPdata").Range("B2:F2").Copy
Sheets("WIPTX").Activate
Sheets("WIPTX").Range(Cells(idxassigned, 1), Cells(idxassigned, 5)).PasteSpecial
idxassigned = idxassigned + 1

Case "On Hold"
Sheets("WIPdata").Range("B2:F2").Copy
Sheets("WIPTX").Activate
Sheets("WIPTX").Range(Cells(idxonhold, 16), Cells(idxonhold, 20)).PasteSpecial
idxonhold = idxonhold + 1

Case "Cancelled"
Sheets("WIPdata").Range("B2:F2").Copy
Sheets("WIPTX").Activate
Sheets("WIPTX").Range(Cells(idxcancelled, 26), Cells(idxcancelled, 30)).PasteSpecial
idxcancelled = idxcancelled + 1

Case "Accepted"
Sheets("WIPdata").Range("B2:F2").Copy
Sheets("WIPTX").Activate
Sheets("WIPTX").Range(Cells(idxaccepted, 6), Cells(idxaccepted, 10)).PasteSpecial
idxaccepted = idxaccepted + 1

Case "Completed"
Sheets("WIPdata").Range("B2:F2").Copy
Sheets("WIPTX").Activate
Sheets("WIPTX").Range(Cells(idxcompleted, 21), Cells(idxcompleted, 25)).PasteSpecial
idxcompleted = idxcompleted + 1
Case Else


End Select

Next cell

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM