|
本帖最后由 xdragon 于 2013-12-2 17:52 编辑
VBA方法哈:- Sub test()
- Dim name, r As Integer
- Dim Conn As Object, Rst As Object
- Dim strConn As String, SQL As String
- Dim i As Integer, PathStr As String
- Set Conn = CreateObject("ADODB.Connection")
- Set Rst = CreateObject("ADODB.Recordset")
- PathStr = ThisWorkbook.FullName
- Select Case Application.Version * 1
- Case Is <= 11
- strConn = "Provider=Microsoft.Jet.Oledb.4.0;Extended Properties=excel 8.0;Data source=" & PathStr
- Case Is >= 12
- strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & PathStr & ";Extended Properties=""Excel 12.0;HDR=YES"";"""
- End Select
-
- r = Sheet1.Cells(Rows.Count, 1).End(3).Row
- name = Sheet1.Cells(2, Sheet1.Range("A2:G" & r).Find([d1]).Column)
- Conn.Open strConn
-
- SQL = "select * from [sheet1$A2:G" & r & "] where " & name & "='" & [d1] & "'"""
- Set Rst = Conn.Execute(Left(SQL, Len(SQL) - 1))
- Range("A3:G" & Cells(Rows.Count, 1).End(3).Row).ClearContents
- Range("A3").CopyFromRecordset Rst
- Rst.Close
- Conn.Close
- Set Conn = Nothing
- Set Rst = Nothing
- End Sub
复制代码 |
|