我每周都会收到一份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还是很陌生,但是可以使用数组吗?初始化代码的“命令按钮”会使其变慢吗?
谢谢!
海蒂
答案 0 :(得分:1)
从当前不相关人员列表中创建一个数组,并将其用于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
答案 1 :(得分:1)