|
我想把"数据表"中的数据匹配到"查询表"中,用字典比Vlookup快,想学下用SQL怎么写
- 字典
- Sub testDic()
- tt = Timer
-
- Sheets("查询表").Select
- maxRow = Cells(Rows.Count, 1).End(xlUp).Row
- Range("b2").Resize(maxRow - 1, 2).ClearContents
- arr = Sheets("查询表").Range("a1").CurrentRegion
- brr = Sheets("数据表").Range("a1").CurrentRegion
- '将数据表中的数据存入字典
- Set dic = CreateObject("scripting.dictionary") 'key:编号&字段 item:数据
- For i = 2 To UBound(brr)
- For j = 2 To UBound(brr, 2)
- dic(brr(i, 1) & brr(1, j)) = brr(i, j)
- Next
- Next
- tt1 = Timer
- '输出数据
- For i = 2 To UBound(arr)
- For j = 2 To UBound(arr, 2)
- If dic.exists(arr(i, 1) & arr(1, j)) Then
- arr(i, j) = dic(arr(i, 1) & arr(1, j))
- End If
- Next
- Next
-
- Range("a1").Resize(UBound(arr), UBound(arr, 2)) = arr
-
- MsgBox "读取耗时: " & Format(tt1 - tt, "0.0秒") & Chr(13) & "输出耗时: " & Format(Timer - tt1, "0.0秒")
- End Sub
复制代码
- 用SQL怎么写呢?
- Sub testSQL()
- tt = Timer
- Sheets("查询表").Select
- maxRow = Cells(Rows.Count, 1).End(xlUp).Row
- Range("b2").Resize(maxRow - 1, 2).ClearContents
-
- Set cnn = CreateObject("ADODB.Connection")
- cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & _
- ThisWorkbook.FullName & ";Extended Properties='Excel 12.0;HDR=yes'"
- '------------------------------------------------------------------------------------
- ' strSQL = "怎么写SQL语句"
-
- '------------------------------------------------------------------------------------
- Range("E2").CopyFromRecordset cnn.Execute(strSQL)
- cnn.Close: Set cnn = Nothing
-
- MsgBox "耗时: " & Format(Timer - tt, "0.0秒")
-
- End Sub
复制代码
|
|