如果您像数据库一样管理数据并希望从中删除特定行并且可以过滤它们,那么有一个技巧可以加快您的删除过程。与简单的循环过程相比,这非常快:
我比较了不同示例的时间(4806 行)。
- 标准循环删除:2:25
- 范围删除:0:20
- 过滤器删除:0:01
示例:我在“Tabelle5”中有数据,想删除特定行。数据从第 6 行开始。应删除第 1 列中以“OLD#”开头的每一行。
1)这里的标准溶液(最长的时间):
Dim i As Integer, counter As Integer
Dim strToRemove As String, strToRemoveRange As String
strToRemove = "OLD#"
strToRemoveRange = ""
counter = 0
With Tabelle5
For i = .UsedRange.Rows.Count To 6 Step -1
If Mid(.Cells(i, 1).value, 1, 4) = strToRemove Then
.Rows(i).Delete Shift:=xlUp
End If
Next i
End With
2)这里是范围解决方案(中间时间):
Dim i As Integer, counter As Integer
Dim strToRemove As String, strToRemoveRange As String
strToRemove = "OLD#"
strToRemoveRange = ""
counter = 0
With Tabelle5
For i = .UsedRange.Rows.Count To 6 Step -1
If Mid(.Cells(i, 1).value, 1, 4) = strToRemove Then
If strToRemoveRange = "" Then
strToRemoveRange = CStr(i) & ":" & CStr(i)
Else
strToRemoveRange = strToRemoveRange & "," & CStr(i) & ":" & CStr(i)
End If
counter = counter + 1
End If
If counter Mod 25 = 0 Then
If counter > 0 Then
.Range(strToRemoveRange).Delete Shift:=xlUp
strToRemoveRange = ""
counter = 0
End If
End If
Next i
If Len(strToRemoveRange) > 0 Then
'.Range(strToRemoveRange).Delete Shift:=xlUp
End If
End With
3)过滤溶液(最短时间):
Dim i As Integer, counter As Integer
Dim strToRemove As String, strToRemoveRange As String
strToRemove = "OLD#"
strToRemoveRange = ""
counter = 0
With Tabelle5
For i = .UsedRange.Rows.Count To 6 Step -1
If Mid(.Cells(i, 1).value, 1, 4) = strToRemove Then
.Cells(i, 1).Interior.Color = RGB(0, 255, 0)
counter = counter + 1
End If
Next i
If counter > 0 Then
.Rows("5:5").AutoFilter
.AutoFilter.Sort.SortFields.Clear
.AutoFilter.Sort.SortFields.Add( _
Range("A5"), xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(0, 255, 0)
.AutoFilter.Sort.Header = xlYes
.AutoFilter.Sort.MatchCase = False
.AutoFilter.Sort.Orientation = xlTopToBottom
.AutoFilter.Sort.SortMethod = xlPinYin
.AutoFilter.Sort.Apply
.Rows("6:" & CStr(counter + 5)).Delete Shift:=xlUp
.Rows("5:5").AutoFilter
End If
End With
在这里,绿线将被排在最前面,一系列的绿色命中将被整体删除。这是我知道的最快的方法!:-)
我希望它会帮助别人!
最好的问候汤姆