简体   繁体   中英

How to copy a row to another sheet based on if then

I'm trying to write a macro that will copy a row to another sheet if certain values are met. There are multiple possible destinations. This is what I've pieced together, but I'm sure it's messy. Basically, if in column 14 there is "N/A" and in column 8 there is "APP" then copy that to the APP tab. And so on for Angie, Cathy, etc.

Dim ws1 As Worksheet: Set ws1 = ThisWorkbook.Sheets("Reconciliation")
Dim ws2 As Worksheet: Set ws2 = ThisWorkbook.Sheets("APP")
Dim ws3 As Worksheet: Set ws3 = ThisWorkbook.Sheets("Angie")
Dim ws4 As Worksheet: Set ws4 = ThisWorkbook.Sheets("Cathy")
Dim ws5 As Worksheet: Set ws5 = ThisWorkbook.Sheets("Cory")
Dim ws6 As Worksheet: Set ws6 = ThisWorkbook.Sheets("Curt")

For Each i In ws1.Range("A1:A1000")
    If ws1.Cells(i, 14) = "#N/A" Then
        If ws1.Cells(i, 8) = "APP" Then
            ws1.Rows(i).Copy ws2.Rows(ws2.Cells(ws2.Rows.Count, 2).End(xlUp).Row + 1)
        End If
    End If
Next i

This will copy the row if there's a sheet with a matching name:

Dim ws1 As Worksheet: Set ws1 = ThisWorkbook.Sheets("Reconciliation")
Dim ws

For Each i In ws1.Range("A1:A1000").Cells
    If ws1.Cells(i.Row, 14).Value = cverr(2042) Then
            Set ws = Nothing
            On Error Resume Next
            Set ws = ThisWorkbook.Sheets(ws1.Cells(i.Row, 8).Value)
            on error goto 0
            If Not ws Is Nothing Then            
                i.EntireRow.Copy ws.Rows(ws.Cells(ws.Rows.Count, 2).End(xlUp).Row + 1)
            End If
        on error goto 0
    End If
Next i

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