根据一系列条件删除行(复数)

问题描述 投票:1回答:2

自从我使用VBA以来已经有一段时间了。我有一系列独特的值,我想搜索一个表。如果所述表中存在这些值,则删除整行。

我能够循环并删除特定的,奇异的值,但努力与多个。我尝试用Range()和一个数组替换“30ExGEPAc30Q4”(下面的代码),但是不能完全得到它。这是我到目前为止所得到的:

Sub test()

Dim x As Long
Dim lastrow As Long

lastrow = Sheets("LRP").ListObjects("Data_LRP").Range.Rows.Count

Worksheets("LRP").Activate
    For x = lastrow To 1 Step -1
        If Cells(x, 1).Value = "30ExGEPAc30Q4" Then
            Rows(x).Delete
        End If
    Next x
End Sub
excel vba range
2个回答
0
投票

如果我理解正确,那就是你想要实现的;我已经清理了一些不必要的部分,现在你只需要编辑x和lastrow。

Sub test()

Dim x As Long
Dim lastrow As Long

'lastrow = Sheets("LRP").ListObjects("Data_LRP").Range.Rows.Count
x = 1
lastrow = 21


'Worksheets("LRP").Activate
Do While x <= lastrow
'    For x = lastrow To 1 Step -1
        If Cells(x, 1).Value = "30ExGEPAc30Q4" Then
            Rows(x).Delete
            lastrow = lastrow - 1
        Else
            x = x + 1
        End If
'    Next x

Loop
End Sub

0
投票

对于那些好奇的人来说,它最终看起来像这样。感谢您的帮助!

Sub Cull()

Dim sht1 As Worksheet
Dim sht2 As Worksheet
Dim sht1row As Long
Dim sht2row As Long
Dim sht2total As Long
Dim DupID As String

Set sht1 = Worksheets("Data Form")
Set sht2 = Worksheets("LRP")
sht2.Activate
sht2total = Worksheets("LRP").ListObjects("Data_LRP").Range.Rows.Count
sht1row = 33

Do While sht1.Cells(sht1row, 2).Value <> ""

DupID = sht1.Cells(sht1row, 2).Value

    For sht2row = 2 To sht2total

        If DupID = Cells(sht2row, 1).Value Then

            Rows(sht2row).Delete
            sht2row = sht2row - 1
            Exit For

        End If

    Next

    sht1row = sht1row + 1

Loop

End Sub
热门问题
推荐问题
最新问题