代码如下:Sub Find_ADO() '不同工作薄Dim thConn As Object
Dim thRst As Object
Dim i, n As Integer
Dim mSql As String
Dim nowvalue As String
Dim dirpath As String
Dim fullname As Stringdirpath = ThisWorkbook.path & "\test"
'MsgBox dirpath
With Sheets("Sheet1")
For i = 2 To .[a65536].End(xlUp).Row
Set fs = Application.FileSearch
nowvalue = .Range("A" & i)
With fs
.LookIn = dirpath
.SearchSubFolders = True
.filename = "*.xls"
If .Execute() > 0 Then
'MsgBox "There were " & .FoundFiles.Count & " file(s) found."
For n = 1 To .FoundFiles.Count
Set thConn = CreateObject("Adodb.Connection")
Set thRst = CreateObject("Adodb.Recordset")
thConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & .FoundFiles(n) & ";" & "Extended Properties=""Excel 8.0;HDR=YES;"""
mSql = "select mfg from [wt1951a$] where barcode = '" & nowvalue & "'"
Set thRst = thConn.Execute(mSql)
If thRst.EOF = False Then
Sheets(1).Cells(i, 2).CopyFromRecordset thRst
thRst.Close
thConn.Close
Set thRst = Nothing
Set thConn = Nothing
Exit For
Else
thRst.Close
thConn.Close
Set thRst = Nothing
Set thConn = Nothing
End If
Next
Else
MsgBox "There were no files found."
End If
End With
Set fs = Nothing
Next i
End With
End Sub
Dim thRst As Object
Dim i, n As Integer
Dim mSql As String
Dim nowvalue As String
Dim dirpath As String
Dim fullname As Stringdirpath = ThisWorkbook.path & "\test"
'MsgBox dirpath
With Sheets("Sheet1")
For i = 2 To .[a65536].End(xlUp).Row
Set fs = Application.FileSearch
nowvalue = .Range("A" & i)
With fs
.LookIn = dirpath
.SearchSubFolders = True
.filename = "*.xls"
If .Execute() > 0 Then
'MsgBox "There were " & .FoundFiles.Count & " file(s) found."
For n = 1 To .FoundFiles.Count
Set thConn = CreateObject("Adodb.Connection")
Set thRst = CreateObject("Adodb.Recordset")
thConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & .FoundFiles(n) & ";" & "Extended Properties=""Excel 8.0;HDR=YES;"""
mSql = "select mfg from [wt1951a$] where barcode = '" & nowvalue & "'"
Set thRst = thConn.Execute(mSql)
If thRst.EOF = False Then
Sheets(1).Cells(i, 2).CopyFromRecordset thRst
thRst.Close
thConn.Close
Set thRst = Nothing
Set thConn = Nothing
Exit For
Else
thRst.Close
thConn.Close
Set thRst = Nothing
Set thConn = Nothing
End If
Next
Else
MsgBox "There were no files found."
End If
End With
Set fs = Nothing
Next i
End With
End Sub
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货