2016-02-07 83 views
0

我有一張名爲Backlog的紙張,其中包含數據的行和列。我需要的代碼將在第二到最後一列中逐行搜索,尋找#N/A。當它發現#N/A時,它需要檢查最後一列是否包含C。如果它包含一個C,那麼整行應該被追加到名爲Logoff的表單中。如果最後一列不包含C,則整行應追加到名爲Denied的表單中。一旦移動到註銷或拒絕狀態,該行應從原始的積壓表中刪除。我下面的代碼不起作用。在第一個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 
+0

鏈接副本顯示如何在列中查找錯誤。複製到另一個工作表是一件簡單的事情。如果遇到麻煩,想辦法複製一行,請回過頭來編輯您的問題以包含您自己的努力。 – Jeeped

+1

我添加了我的代碼。我無法弄清楚爲什麼在For語句後它不起作用。當我調試它時,立即在For i = 3語句後跳到End,並且不會評估任何行。 –

回答

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 
+0

謝謝Jeeped。 AutoFilter代碼完美運行。 –

相關問題