如果符合条件,则删除for循环中的行

时间:2019-07-15 05:04:36

标签: vba for-loop listobject

将行复制到另一个位置后,我想删除它。这个问题有很多帖子。我不确定为什么他们都不在我的特定情况下工作。

http://localhost:port: Invalid data found when processing input

1 个答案:

答案 0 :(得分:1)

尝试以下代码:

Sub Transition_Queue_to_Other()

Dim QueueSheet As Worksheet
Set QueueSheet = ThisWorkbook.Worksheets("Project Queue")

Dim TableQueue As ListObject
Set TableQueue = QueueSheet.ListObjects("TableQueue")

Dim TransColumn As Range
Set TransColumn = QueueSheet.Range("TableQueue[Transition]")

Dim Trans_Queue_Row As Range
Dim i As Integer


            Dim NPDSheet As Worksheet
            Set NPDSheet = ThisWorkbook.Worksheets("NPD")

            Dim TableNPD As ListObject
            Set TableNPD = NPDSheet.ListObjects("TableNPD")



With TransColumn
    For i = .Count To 1 Step -1

        If InStr(1, .Rows(i).Value, "NPD") > 0 Then

            Set Trans_Queue_Row = TableQueue.DataBodyRange.Rows(i)
            Set Trans_NPD_Row = TableNPD.ListRows.Add.Range

            Trans_NPD_Row.Cells(, 1).Value = Trans_Queue_Row.Cells(, 2).Value

            Trans_Queue_Row.EntireRow.Delete xlShiftUp

        End If
    Next
End With

End Sub

从下到上并使用EntireRow.Delete

删除行