#3Created循环永远不会结束,必须删除单元格的第一个字符 [英] must remove first character of cell if not #3Created loop never ends
问题描述
因此,基本上,我需要删除主键字段中第二位不为3的所有记录,例如,它们可能看起来像这样
So basically, I need to remove all records that don't have 3 as the 2nd digit in the primary key field which for example can either look like this
#39001
或者没有#3
我想要的是所有非#3的单元格都开始,它们的行被删除,我想出了以下代码,它删除了所有代码,但是宏从未停止运行..
What I want is that all cells that have a non #3 start, their rows be deleted I came up with the following code, It removes all, but the macro never stops running..
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
有什么想法吗?
谢谢
推荐答案
尝试以下方法:
Sub keep3()
Dim i As Integer
For i = 14000 To 2 Step -1
If Mid(Cells(i, 2), 2, 1) = "3" Then
Rows(i).EntireRow.Delete
End If
Next
End Sub
请注意,循环正在倒退-这是因为当您删除一行时,下面的行会被拉起,因此,如果您前进然后删除一行,您的循环可能会发生奇怪的事情,因为您可能会跳过您可能还想删除的下一行.另外,我更改了IF语句,就像您说的那样,您要删除第二个字符为3的行.
Note that the loop is going backwards - this is because when you delete a row, the row below gets pulled up, so your loop can do strange things if you go forwards and then delete a row, as you may skip over the next row that you may have also wanted to delete. Also, I changed the IF statement as you said you want to delete rows where the 2nd character is a 3.
这篇关于#3Created循环永远不会结束,必须删除单元格的第一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!