1

所以基本上,我需要删除所有没有 3 作为主键字段中第二位数字的记录,例如,它们可能看起来像这样

#39001

或者没有#3

我想要的是所有具有非#3 开始的单元格,它们的行被删除我想出了以下代码,它删除了一些非#3,但不是全部。我有超过 13000 行要扫描

Sub keep3()
'
' RemoveNum Macro
Dim i As Integer

For i = 2 To 14000

If InStr(Cells(i, 2), "3") = 2 Then

Else
Rows(i).EntireRow.Delete
End If
Next i
End Sub

如果您能指出我正确的方向,我将不胜感激!

谢谢!

4

2 回答 2

3

当您删除一行时,它下面的所有行都会向上移动,然后您增加“i”,这实际上会跳过新移动的行。请尝试以下操作...

Sub keep3()
'
' RemoveNum Macro
Dim i As Integer
i = 2
While i < 14000
    If InStr(Cells(i, 2), "3") = 2 Then
        ' Do nothing
        i = i + 1
    Else
        Rows(i).EntireRow.Delete
        ' Note - not incrementing i here
    End If
Wend
End Sub
于 2013-05-14T18:17:17.520 回答
0

2 possibilities:

Remove the line that has the Else. I think it is preventing the delete from working in the way you want. I am assuming that you do not have more than 14000 lines of data....

Or it might be that when you delete the row it messes up the order of the rows that you are running through.

于 2013-05-14T18:09:49.993 回答