Find cells on one sheet and copy the rows to another sheet

后端 未结 1 1142
误落风尘
误落风尘 2021-01-29 05:39

I have a sheet called Backlog containing rows and columns of data. I need code that will search row by row in the 2nd to last column looking for #N/A. When it finds #N/A it ne

相关标签:
1条回答
  • 2021-01-29 05:44

    Try it as If Cells(i, 13).Text = "#N/A" Then . #N/A is an error code, not a value; however, the Range.Text property can be examined or the IsError function could be used to examine the cell's contents for any error.

        If Cells(i, 13).Text = "#N/A" Then
        'Alternate with IsError
        'If IsError(Cells(i, 13)) Then
            If Cells(i, 14).Value = "C" Then
                IMBacklogSh.Rows(i).EntireRow.Copy _
                    Destination:=logoffSh.Range("A" & logoffsh.Cells(Rows.Count, "A").End(xlUp).Row + 1)
            Else
                IMBacklogSh.Rows(i).EntireRow.Copy _
                    Destination:=deniedsh.Range("A" & deniedsh.Cells(Rows.Count, "A").End(xlUp).Row + 1)
            End If
        End If
    

    However, individual cell examination is not necessary and time consuming. The AutoFilter method can be used to isolate #N/A with C and #N/A with <>C.

    Private Sub CommandButton2_Click()
        Dim IMBacklogSh As Worksheet, logoffSh As Worksheet, deniedsh As Worksheet
    
        Set IMBacklogSh = ThisWorkbook.Worksheets("Backlog")
        Set logoffSh = ThisWorkbook.Worksheets("Claims Logged off")
        Set deniedsh = ThisWorkbook.Worksheets("Claims Denied")
    
        With IMBacklogSh
            If .AutoFilterMode Then .AutoFilterMode = False
            With .Cells(1, 1).CurrentRegion
                .AutoFilter field:=13, Criteria1:="#N/A"
                .AutoFilter field:=14, Criteria1:="C"
                With .Resize(.Rows.Count - 1, Columns.Count).Offset(1, 0)
                    If CBool(Application.Subtotal(103, .Cells)) Then
                        .Copy Destination:= _
                            logoffSh.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
                        'optionally delete the originals
                        .EntireRow.Delete
                    End If
                End With
                .AutoFilter field:=14, Criteria1:="<>C"
                With .Resize(.Rows.Count - 1, Columns.Count).Offset(1, 0)
                    If CBool(Application.Subtotal(103, .Cells)) Then
                        .Copy Destination:= _
                            deniedsh.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
                        'optionally delete the originals
                        .EntireRow.Delete
                    End If
                End With
            End With
            If .AutoFilterMode Then .AutoFilterMode = False
        End With
    End Sub
    
    0 讨论(0)
提交回复
热议问题