VBA 搜索值并从列表中删除(for 循环太慢)
VBA search for values and delete from list (for loop is too slow)
我每周都会收到一份 Excel 文件,其中包含尚未完成我们正在获得的新 IT 系统培训的员工名单。但是,培训并不适合所有人,因此我需要清理以获得真实数字。
我已经列出了与培训无关的员工名单 (Oprydning),并使用以下代码一次搜索一个姓名并从另一个名单 (Datagrundlag) 中删除。
Private Sub RydOpKnap_Click()
Dim OprydningAntal As Long
Dim DataAntal As Long
Dim Find As String
Worksheets("Datagrundlag - endelig").Visible = True
OprydningsAntal = Worksheets("Oprydning").UsedRange.Rows.Count
DataAntal = Worksheets("Datagrundlag - endelig").UsedRange.Rows.Count
Dim r, s As Long
For r = 2 To OprydningsAntal
Find = Worksheets("Oprydning").Cells(r, 1).Value
For s = 2 To DataAntal
If Worksheets("Datagrundlag - endelig").Cells(s, 3).Value = Find Then
Worksheets("Datagrundlag - endelig").Cells(s, 3).EntireRow.Delete
Exit For
End If
Next s
Next r
Worksheets("Datagrundlag - endelig").Visible = False
ActiveWorkbook.RefreshAll
End Sub
但这需要很长时间,因为不相关的员工列表目前有 460 个不同的值(而且会越来越大)。还有另一种方法吗?我对 VBA 很陌生,但是可以使用数组吗?启动代码的 "Command Button" 是否使其变慢?
提前致谢!
海蒂
根据您当前的非相关人员列表创建一个数组,并将其用于带有 xlFilterValues 的自动筛选。删除可见行并删除过滤器。
Option Explicit
Private Sub RydOpKnap_Click()
Dim i As Long, j As Long
ReDim notRelevant(0) As Variant
With Worksheets("Oprydning")
For i = 2 To .Cells(.Rows.Count, "A").End(xlUp).Row
ReDim Preserve notRelevant(i-2)
notRelevant(i - 2) = .Cells(i, "A").Value
Next i
End With
With Worksheets("Datagrundlag - endelig")
.Visible = True
If .AutoFilterMode Then .AutoFilterMode = False
With .Range(.Cells(1, "C"), .Cells(.Rows.Count, "C").End(xlUp))
.AutoFilter field:=1, Criteria1:=(notRelevant), Operator:=xlFilterValues
.Offset(1, 0).EntireRow.Delete
End With
.AutoFilterMode = False
.Visible = False
.Parent.RefreshAll
End With
End Sub
Is there another way to do this?
是的,使用条件格式为要排除的那些设置颜色 eg,并过滤以删除带有格式化单元格的行。
我每周都会收到一份 Excel 文件,其中包含尚未完成我们正在获得的新 IT 系统培训的员工名单。但是,培训并不适合所有人,因此我需要清理以获得真实数字。
我已经列出了与培训无关的员工名单 (Oprydning),并使用以下代码一次搜索一个姓名并从另一个名单 (Datagrundlag) 中删除。
Private Sub RydOpKnap_Click()
Dim OprydningAntal As Long
Dim DataAntal As Long
Dim Find As String
Worksheets("Datagrundlag - endelig").Visible = True
OprydningsAntal = Worksheets("Oprydning").UsedRange.Rows.Count
DataAntal = Worksheets("Datagrundlag - endelig").UsedRange.Rows.Count
Dim r, s As Long
For r = 2 To OprydningsAntal
Find = Worksheets("Oprydning").Cells(r, 1).Value
For s = 2 To DataAntal
If Worksheets("Datagrundlag - endelig").Cells(s, 3).Value = Find Then
Worksheets("Datagrundlag - endelig").Cells(s, 3).EntireRow.Delete
Exit For
End If
Next s
Next r
Worksheets("Datagrundlag - endelig").Visible = False
ActiveWorkbook.RefreshAll
End Sub
但这需要很长时间,因为不相关的员工列表目前有 460 个不同的值(而且会越来越大)。还有另一种方法吗?我对 VBA 很陌生,但是可以使用数组吗?启动代码的 "Command Button" 是否使其变慢?
提前致谢!
海蒂
根据您当前的非相关人员列表创建一个数组,并将其用于带有 xlFilterValues 的自动筛选。删除可见行并删除过滤器。
Option Explicit
Private Sub RydOpKnap_Click()
Dim i As Long, j As Long
ReDim notRelevant(0) As Variant
With Worksheets("Oprydning")
For i = 2 To .Cells(.Rows.Count, "A").End(xlUp).Row
ReDim Preserve notRelevant(i-2)
notRelevant(i - 2) = .Cells(i, "A").Value
Next i
End With
With Worksheets("Datagrundlag - endelig")
.Visible = True
If .AutoFilterMode Then .AutoFilterMode = False
With .Range(.Cells(1, "C"), .Cells(.Rows.Count, "C").End(xlUp))
.AutoFilter field:=1, Criteria1:=(notRelevant), Operator:=xlFilterValues
.Offset(1, 0).EntireRow.Delete
End With
.AutoFilterMode = False
.Visible = False
.Parent.RefreshAll
End With
End Sub
Is there another way to do this?
是的,使用条件格式为要排除的那些设置颜色 eg,并过滤以删除带有格式化单元格的行。