|
在一个10000行*60列的表中进行筛选,选用AutoFilter、For Each、数组三种方法,结果证明数组法并不比For Each快多少,而且思路比较复杂。数组法之所以没有太大优势,可能是因为反复的使用Union命令的缘故,但数组在Excel中的使用时,常常最终会落实到Range上。请问有什么好办法绕开类似Union这类环节吗?
本帖最后由 cbg2008 于 2013-2-15 20:09 编辑
为了证明数组是快的,很懒的我写了这段代码,你测一下,真正的“秒杀”,如果不用UNION,只要把符合条件的数据赋值到新区域,那可以更快。 - Sub 高效数组方法()
- Dim arr, t
- Dim x As Integer
- Dim sr As String, sr1 As String
- Dim Rws As Range
- t = Timer
- arr = Range("B1:B" & Range("a65536").End(xlUp).Row)
- For x = 1 To UBound(arr)
- If x = UBound(arr) Then
- If Rws Is Nothing Then
- Set Rws = Range(Mid(sr, 2, 255))
- Else
- Set Rws = Union(Rws, Range(Mid(sr, 2, 255)))
- End If
- End If
- If InStr(arr(x, 1), "一") > 0 Then
- sr1 = sr
- sr = sr & "," & x & ":" & x
- If Len(sr) > 255 Then
- x = x - 1
- sr = sr1
- If Rws Is Nothing Then
- Set Rws = Range(Mid(sr, 2, 255))
- Else
- Set Rws = Union(Rws, Range(Mid(sr, 2, 255)))
- End If
- sr = ""
- End If
- End If
- Next x
- MsgBox Timer - t
- ' Rws.Interior.ColorIndex = 3 '去掉注释可以测试筛选结果是否正确
- End Sub
复制代码
|
|