|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
本帖最后由 xcc324919 于 2015-12-21 10:29 编辑
看了gbgbxgb老师的图片感觉很有趣,不知道gbgbxgb是如何实现了,自己模拟了一个,基本上能到到gbgbxgb老师的功能,随意添加查询字段。献上附件仅供参考。烦请多提些建议。小白一个还请见谅,因为不知道如何将数据库数据放入数组,所以还是先用SQL提取所有数据出来再用另一个SQL来进行条件提取。查询字段可以随意添加要查询的东西,如年龄,工资级别等。烦请有优化方案的多多提出,让我等学习下、谢谢!
Sub tq()
Sheet1.[a5:g65536].ClearContents
Sheet1.[ad1:aj65536].ClearContents
Dim arr, b, brr, a, x
arr = Sheet1.[a1:g2]
For a = 1 To 7
If arr(1, a) = "" Then
arr(1, a) = "'%'"
End If
If arr(2, a) = "" Then
arr(2, a) = "%"
End If
If arr(2, a) <> "" Then
arr(2, a) = "'" & arr(2, a) & "'"
End If
Next
Dim rs As Object, i As Long
Set Conn = CreateObject("ADODB.Connection") '''''设置
PathStr = ThisWorkbook.FullName '''''要引用工作表位置
Conn.Open "Provider=Microsoft.Jet.Oledb.4.0;Extended Properties=excel 8.0;Data source=" & PathStr ''''''打开连接
strSQL = "select * from [A公司$a1:bz] union all select * from [B公司$a1:bz] union all select * from [C公司$a1:bz] union all select * from [D公司$a1:bz]" ''''升序使用 asc ,降序使用desc
Set rs = Conn.Execute(strSQL)
For i = 0 To rs.fields.Count - 1
Sheet1.Cells(1, i + 30) = rs.fields(i).Name
Next
Sheet1.[AD2].CopyFromRecordset Conn.Execute(strSQL)
Conn.Close
Set cnn = CreateObject("adodb.connection")
cnn.Open "provider=microsoft.jet.oledb.4.0;extended properties=excel 8.0;data source=" & ThisWorkbook.FullName
Sql = "select * from [筛选结果$ad1:aj] where " & arr(1, 1) & " like" + arr(2, 1) + "" & " and " & arr(1, 2) & " like " + arr(2, 2) + "" & " and " & arr(1, 3) & " like " + arr(2, 3) + "" & " and " & arr(1, 4) & " like " + arr(2, 4) + "" & " and " & arr(1, 5) & " like " + arr(2, 5) + "" & " and " & arr(1, 6) & " like " + arr(2, 6) + "" & " and " & arr(1, 7) & " like " + arr(2, 7) + ""
Sheet1.[a5].CopyFromRecordset cnn.Execute(Sql)
cnn.Close
End Sub
后面空白处还可以加入其它想进行筛选的字段来进行筛选
|
评分
-
1
查看全部评分
-
|