|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
用数据库的方法也做了一下,发现运行时间比较慢。
- Sub fyExcelVBACon()
- Dim arr, brr, arr1, i&, j%, nRow%, nCol%, str$
- Dim dic As Object
- Dim cnn, SQL$ '定义数据库连接和SQL语句
- t = Timer
- Application.ScreenUpdating = False
- Application.DisplayAlerts = False
- '--------------------------------------- SQL结果处理 ------------------- ------------------- -------------------
- With Sheets("sheet1")
- nRow = .Range("a" & Rows.Count).End(3).Row
- nCol = .Cells(1, Columns.Count).End(1).Column
- arr = .Range("a1").Resize(nRow, nCol - 1)
- brr = .Range(.Cells(1, nCol), .Cells(nRow, nCol))
- End With
- '--------------------------------------- 设置数据库连接 ------------------- ------------------- -------------------
- Set dic = CreateObject("scripting.dictionary")
- Set cnn = CreateObject("adodb.connection") '创建数据库连接
- Set rs = CreateObject("adodb.recordset") '创建一个数据集保存数据
- cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" & ThisWorkbook.FullName
- '--------------------------------------- SQL查询语句 ------------------- ------------------- -------------------
- SQL = "SELECT * FROM [Sheet1$] where 本月出勤天数(B列)>= 20 And 本月出勤工时(C列) >= 180 ORDER BY 本月业绩(D列) DESC"
- Set rs = cnn.Execute(SQL) '将SQL语句获得的数据传递给数据集
- '--------------------------------------- SQL结果处理 ------------------- ------------------- -------------------
- arr1 = rs.getrows '将表数据传给数组
- For i = 0 To UBound(arr1)
- n = n + 1
- dic(arr1(0, i)) = n '字典记录排名
- Next i
- For i = 2 To UBound(brr) '查字典获取排名
- If Not dic.exists(arr(i, 1)) Then
- brr(i, 1) = ""
- Else
- brr(i, 1) = dic(arr(i, 1))
- End If
- Next i
- With Sheets("sheet1")
- .Range(.Cells(1, nCol), .Cells(nRow, nCol)).ClearContents
- .Range(.Cells(1, nCol), .Cells(nRow, nCol)) = brr
- End With
- rs.Close '关闭数据集
- cnn.Close '关闭数据库连接
- Set rs = Nothing
- Set cnn = Nothing '将CNN从内存中删除。
- Set dic = Nothing
- Application.ScreenUpdating = True
- Application.DisplayAlerts = True
- MsgBox Timer - t
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|