VBA三种查询方式整理

效果图


下面是我这两天整理的三种查询代码,效果各不相同,不以实用为出发,只以单纯的学习为目的。

Option Explicit

Sub 普通查询()

Dim rng As Range, t

t = Timer

Range("f2:h10000").ClearContents

For Each rng In Range("a2", [a2].End(xlDown))

If rng.Value = [f1].Value Then

[f63356].End(xlUp).Offset(1, 0).Resize(1, 3) = rng.Resize(1, 3).Value

End If

Next

MsgBox "运行耗用时间为" & Format(Timer - t, "0.0000" & "秒")

End Sub

Sub find查询()

Dim rng As Range, adss, t

t = Timer

Range("f2:h10000").ClearContents

Set rng = Range("a:a").Find(Cells(1, "f").Value)

adss = rng.Address

Do

Set rng = [a:a].FindNext(rng)

rng.EntireRow.Range("a1:c1").Copy Cells(63356, "f").End(xlUp).Offset(1, 0)

Loop Until adss = rng.Address

MsgBox "运行耗用时间为" & Format(Timer - t, "0.0000" & "秒")

End Sub

Sub 数组查询()'join与split函数只能处理一维数组,且数组中的值会转化为字符型

Dim arr(), rng As Range, arr1, ar, arr2(), n%, m%, t

t = Timer

Range("f2:h10000").ClearContents

For Each rng In Range("a2", [a63356].End(xlUp))

n = n + 1

ReDim Preserve arr(1 To n)

arr(n) = Join(Application.Transpose(Application.Transpose(rng.Resize(1, 3))))

Next

arr1 = Filter(arr, [f1])

ReDim Preserve arr2(1 To UBound(arr1) + 1)

For Each ar In arr1

m = m + 1

arr2(m) = Split(ar)

Next

[f2].Resize(m, 3) = Application.Transpose(Application.Transpose(arr2))

MsgBox "运行耗用时间为" & Format(Timer - t, "0.0000" & "秒")

End Sub

你可能感兴趣的:(VBA三种查询方式整理)