|
Option Explicit
Sub test1()
Dim ar, br, i&, cnn As Object, rst As Object, strCnn$, strSQL$, strPath$
Set cnn = CreateObject("ADODB.Connection")
Set rst = CreateObject("ADODB.Recordset")
strPath = ThisWorkbook.FullName
Select Case Application.Version * 1
Case Is <= 11
strCnn = "Provider=Microsoft.Jet.Oledb.4.0;Extended Properties=excel 8.0;Data Source=" & strPath
Case Is >= 12
strCnn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strPath & ";Extended Properties=""Excel 12.0;HDR=YES"";"""
End Select
cnn.Open strCnn
ar = [H3].CurrentRegion.Value
br = Split(ar(1, 2), "和")
strSQL = "Select * From [sheet1$A3:E] Where (" & ar(1, 1) & " like '" & br(0) & "%' or " & ar(1, 1) & _
" like '" & br(1) & "%') and " & ar(2, 1) & "='" & ar(2, 2) & "' and " & ar(3, 1) & "='" & ar(3, 2) & "'"
Set rst = cnn.Execute(strSQL)
With ThisWorkbook.Sheets(2)
.Cells.Clear
For i = 0 To rst.Fields.Count - 1
.Cells(1, i + 1) = rst.Fields(i).Name
Next i
.Range("A2").CopyFromRecordset rst
.Cells.EntireColumn.AutoFit
.Activate
End With
rst.Close: cnn.Close
Set rst = Nothing: Set cnn = Nothing
Beep
End Sub
|
评分
-
1
查看全部评分
-
|