在一张纸上查找单元格并将行复制到另一张纸

时间:2016-02-07 23:21:09

标签: excel vba excel-vba copy-paste autofilter

我有一张名为Backlog的工作表,其中包含行和列数据。我需要在第2到第2列中逐行搜索的代码,以查找#N / A.当它找到#N / A时,如果它包含C,则需要检查最后一列。如果它包含C,那么整行应该附加到名为Logoff的工作表中。如果最后一列不包含C,则应将整行附加到名为Denied的工作表中。一旦移动到Logoff或Denied,就应该从原始Backlog表中删除该行。我下面的代码不起作用。在第一个For语句之后,它转到End Sub,但没有任何编译错误。

Private Sub CommandButton2_Click()
    Dim IMBacklogSh As Worksheet
    Set IMBacklogSh = ThisWorkbook.Worksheets("Backlog")
    Dim logoffSh As Worksheet
    Set logoffSh = ThisWorkbook.Worksheets("Claims Logged off")
    Dim deniedsh As Worksheet
    Set deniedsh = ThisWorkbook.Worksheets("Claims Denied")

    IMBacklogSh.Select
    Dim i As Long
    For i = 3 To Cells(Rows.Count, 13).End(xlUp).Row
        If Cells(i, 13).Value = "#N/A" 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
    Next i
End Sub

1 个答案:

答案 0 :(得分:3)

尝试If Cells(i, 13).Text = "#N/A" Then#N/A是错误代码,不是值;但是,可以检查Range.Text property,或者可以使用IsError function检查单元格内容是否有任何错误。

    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

然而,单个细胞检查不是必需且耗时的。 AutoFilter method可用于将#N/AC#N/A<>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