這使用數組並且相當快。
Sub foo()
Dim tickSht As Worksheet
Dim restSht As Worksheet
Dim disaSht As Worksheet
Dim tickArr() As Variant
Dim restArr() As Variant
Dim disaArr() As Variant
Dim outArr() As Variant
Dim i&, k&, j&, r&, d&
Dim dishr As Boolean
Dim tichr As Boolean
Set tickSht = ThisWorkbook.Worksheets("Tickers") 'ensure that this is the correct sheet name
Set restSht = ThisWorkbook.Worksheets("Restricted") 'ensure that this is the correct sheet name
Set disaSht = ThisWorkbook.Worksheets("Disabled") 'ensure that this is the correct sheet name
'load arrays
'if you have a title row then change the "A1" to "A2" or the first row.
'If your data is in a differect column then change the column.
With disaSht
disaArr = .Range("A1", .Cells(.Rows.Count, 1).End(xlUp)).Value
End With
With restSht
restArr = .Range("A1", .Cells(.Rows.Count, 1).End(xlUp)).Value
End With
r = Application.Evaluate("SUM(countifs(" & tickSht.Range("A1", tickSht.Cells(tickSht.Rows.Count, 1).End(xlUp)).Address & _
"," & restSht.Range("A1", restSht.Cells(restSht.Rows.Count, 1).End(xlUp)).Address & "))")
d = Application.Evaluate("SUM(countifs(" & tickSht.Range("A1", tickSht.Cells(tickSht.Rows.Count, 1).End(xlUp)).Address & _
"," & disaSht.Range("A1", disaSht.Cells(disaSht.Rows.Count, 1).End(xlUp)).Address & "))")
With tickSht
tickArr = .Range("A1", .Cells(.Rows.Count, 1).End(xlUp)).Value
ReDim outArr(1 To UBound(tickArr, 1) - d - t, 1 To 1)
k = 1
For i = LBound(tickArr, 1) To UBound(tickArr, 1)
dishr = False
tichr = False
For j = LBound(disaArr, 1) To UBound(disaArr, 1)
If disaArr(j, 1) = tickArr(i, 1) Then dishr = True
Next j
For j = LBound(restArr, 1) To UBound(restArr, 1)
If restArr(j, 1) = tickArr(i, 1) Then tichr = True
Next j
If Not tichr And Not dishr Then
outArr(k, 1) = tickArr(i, 1)
k = k + 1
End If
Next i
.Range("B1").Resize(UBound(outArr, 1), 1).Value = outArr
End With
End Sub
這假定數據在所有三張表的A列中,並且沒有標題行。如果不同,那麼需要進行一些調整。
這是動態的,因爲它始終會發現所有三張紙上的數據範圍都會將它們加載到數組中並遍歷這些數據。
數組的使用限制了vba在excel中訪問工作表的次數,因此對於較大的數據集它會更快。
你試過了什麼 - 任何代碼? – jcoppens