[英]Delete rows if value is not mentioned in range
Out of a file with approximately 50.000 rows I want to delete rows which don't have a specific number in column B. I use this code: 在大约50.000行的文件中,我要删除B列中没有特定编号的行。我使用以下代码:
Sub DelRows()
Application.ScreenUpdating = False
Worksheets("2016").Activate
lastrow = Cells(Rows.Count, "A").End(xlUp).Row
For i = lastrow To 2 Step -1
If Cells(i, "B").Value <> "1060" And _
Cells(i, "B").Value <> "1061" And _
Cells(i, "B").Value <> "1062" And _
Cells(i, "B").Value <> "1063" And _
Cells(i, "B").Value <> "1064" And _
Cells(i, "B").Value <> "1105" And _
Cells(i, "B").Value <> "11050" And _
Cells(i, "B").Value <> "11051" And _
Cells(i, "B").Value <> "11053" And _
Cells(i, "B").Value <> "11054" And _
Cells(i, "B").Value <> "1160" And _
Cells(i, "B").Value <> "1161" And _
Cells(i, "B").Value <> "1162" And _
Cells(i, "B").Value <> "1163" And _
Cells(i, "B").Value <> "1164" And _
Cells(i, "B").Value <> "1166" And _
Cells(i, "B").Value <> "1168" And _
Cells(i, "B").Value <> "1169" And _
Cells(i, "B").Value <> "8060" And _
Cells(i, "B").Value <> "8061" And _
Cells(i, "B").Value <> "8062" And _
Cells(i, "B").Value <> "8063" And _
Cells(i, "B").Value <> "8064" And _
Cells(i, "B").Value <> "8068" And _
Cells(i, "B").Value <> "8192" Then
Cells(i, "B").EntireRow.Delete
End If
Next i
End Sub
This macro takes a lot of time and it seems to be that there is a maximum of 'and-statements'. 这个宏会花费很多时间,而且似乎存在最多的“与陈述”。
I tried to figure it out with an array or a filter, but it's hard for me as a beginner. 我试图用数组或过滤器来解决这个问题,但是对于初学者来说,这很难。
I would like to put the numbers on a separate worksheet as a range eg: 我想将数字放在一个单独的工作表中作为范围,例如:
A
1 1060
2 1061
3 1062
4 1063
5 1064
…
I've tried to figure it out with section Criteria range on a different sheet* on https://www.rondebruin.nl/win/winfiles/MoreDeleteCode.txt , but I don't fully understand this VBA code. 我已经尝试在https://www.rondebruin.nl/win/winfiles/MoreDeleteCode.txt 的另一张纸上的“条件范围”一节中找到标准 ,但我并不完全理解此VBA代码。
Can somebody please help me? 有人能帮帮我吗? Kind regards, Richard 亲切的问候,理查德
Let's say the values are as in the code below - rngCheck
and rngDelete
. 假设值与rngCheck
和rngDelete
下面的代码rngDelete
。
A nested loop can do exactly this job. 嵌套循环可以完全完成此工作。 The outer loop goes through the range, which should be deleted rngDelete
and the inner goes through the checking values rngCheck
. 外循环遍历该范围,应将其删除rngDelete
,内循环遍历检查值rngCheck
。
If a matching value is found, it is deleted and the inner loop is exited. 如果找到匹配的值,则将其删除并退出内部循环。 As far as we are looping through rows and we need to delete some of them, the for loop is with reversed counting: 就我们遍历行并需要删除其中一些行而言,for循环具有反向计数:
Option Explicit
Public Sub TestMe()
Dim cnt As Long
Dim rngDelete As Range
Dim rngCheck As Range
Dim rngCell As Range
Set rngCheck = Worksheets(2).Range("A1:A2")
Set rngDelete = Worksheets(1).Range("A1:A20")
For cnt = rngDelete.Rows.Count To 1 Step -1
For Each rngCell In rngCheck
If rngCell = rngDelete.Cells(cnt, 1) Then
rngDelete.Rows(cnt).Delete
Exit For
End If
Next rngCell
Next cnt
End Sub
Here's an array approach which saves on reading from and writing to spreadsheets and so should be a bit quicker. 这是一种数组方法,可节省读取和写入电子表格的时间,因此应更快一些。 This method includes the cells which do match rather than excluding those which don't. 此方法包括匹配的单元格,而不是排除不匹配的单元格。 Adjust your range of cells against which you are checking accordingly. 相应地调整要检查的单元格范围。 I have assumed your data start in A1 of sheet 2016. 我假设您的数据从表2016的A1开始。
Sub DelRows()
Dim v, i As Long, j As Long, vOut(), k As Long, rExcl As Range
Set rExcl = Sheets("Sheet2").Range("A1:A5") 'adjust accordingly
With Worksheets("2016")
v = .Range("A1").CurrentRegion.Value
.Range("A1").CurrentRegion.Offset(1).ClearContents
ReDim vOut(1 To UBound(v, 1), 1 To UBound(v, 2))
For i = LBound(v, 1) To UBound(v, 1)
If IsNumeric(Application.Match(v(i, 2), rExcl, 0)) Then
j = j + 1
For k = LBound(v, 2) To UBound(v, 2)
vOut(j, k) = v(i, k)
Next k
End If
Next i
.Range("A2").Resize(j, UBound(v, 2)) = vOut
End With
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.