|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
本帖最后由 zhaogang1960 于 2012-7-31 11:20 编辑
优化一下,使用一个数组arr:- Sub Find法()
- Dim c As Range, firstAddress$, arr(), m&, n&, temp$
- temp = [c1]
- n = WorksheetFunction.CountIf(Range("a:a"), temp)
- ReDim arr(1 To [a65536].End(xlUp).Row, 1 To 1)
- With Range("a:a")
- Set c = .Find(temp, , , 2)
- If Not c Is Nothing Then
- firstAddress = c.Address
- Do
- If c.Value = temp Then
- m = m + 1
- arr(m, 1) = c.Offset(, 1)
- Else
- n = n + 1
- arr(n, 1) = c.Offset(, 1)
- End If
- Set c = .FindNext(c)
- Loop While Not c Is Nothing And c.Address <> firstAddress
- End If
- End With
- Range("d2:d65536").ClearContents
- If n > 0 Then [d2].Resize(n) = arr
- End Sub
复制代码 |
|