|
Sub ADO法_一薄一列转一行_参考()
Dim Fso As Object, File As Object, cnn As Object, rs As Object, SQL$, t$, m&, arr(), i&
Application.ScreenUpdating = False
Set Fso = CreateObject("Scripting.FileSystemObject")
Set cnn = CreateObject("adodb.connection")
ReDim arr(1 To Fso.GetFolder(ThisWorkbook.Path).Files.Count, 0 To 4)
On Error Resume Next
For Each File In Fso.GetFolder(ThisWorkbook.Path).Files
If File.Name Like "*.xlsx" Then
m = m + 1
If m = 1 Then
cnn.Open "provider=microsoft.ace.oledb.12.0;extended properties='Excel 12.0;imex=1;hdr=no';data source=" & File
Else
t = "[Excel 12.0;imex=1;hdr=no;Database=" & File & ";]."
End If
SQL = "select f1 from " & t & "[Sheet1$b1:b5]"
Set rs = cnn.Execute(SQL)
For i = 0 To 4
arr(m, i) = rs.Fields(0).Value
rs.MoveNext
Next
End If
Next
Cells.ClearContents
Range("a1").Resize(m, 5) = arr
rs.Close
cnn.Close
Set rs = Nothing
Set cnn = Nothing
Set Fso = Nothing
Application.ScreenUpdating = True
End Sub |
评分
-
1
查看全部评分
-
|