|
请参考:
- Sub ADO法()
- Dim Fso As Object, File As Object, cnn As Object, rs As Object, SQL$, m&, arr()
- Set Fso = CreateObject("Scripting.FileSystemObject")
- ReDim arr(1 To Fso.GetFolder(ThisWorkbook.Path).Files.Count - 1, 1 To 2)
- On Error Resume Next
- For Each File In Fso.GetFolder(ThisWorkbook.Path).Files
- If File.Name Like "*.xlsx" Then
- m = m + 1
- arr(m, 1) = Replace(File.Name, ".xlsx", "")
- Set cnn = CreateObject("adodb.connection")
- cnn.Open "provider=microsoft.ace.oledb.12.0;extended properties='excel 12.0;hdr=no';data source=" & File
- SQL = "select f7 from [表四甲(主材表)$B7:H] where f1 like '%采购及保管费%'"
- Set rs = CreateObject("adodb.recordset")
- rs.Open SQL, cnn, 1, 3
- If Err.Number = 0 Then
- If Not rs.EOF Then
- arr(m, 2) = rs.Fields(0)
- Else
- arr(m, 2) = 0
- End If
- Else
- arr(m, 2) = 0
- Err.Clear
- End If
- End If
- Next
- ActiveSheet.UsedRange.Offset(1).ClearContents
- Range("A2").Resize(m, 2) = arr
- rs.Close
- cnn.Close
- Set rs = Nothing
- Set cnn = Nothing
- Set Fso = Nothing
- End Sub
复制代码 |
|