|
zhu086 发表于 2014-7-4 11:53
非常感谢老师帮忙,单独放在一个文件夹里,测试可以用,如果三个工作表都放在桌面测试就会出现错误
只有查询"1.csv", "2.csv"两个文件?如果是:- Sub ADO法()
- Dim cnn As Object, rs As Object, SQL$, i&, m&, c
- myPath = ThisWorkbook.Path & ""
- Set cnn = CreateObject("adodb.connection")
- cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='text;FMT=DELIMITED';Data Source=" & myPath
- For Each c In Array("1.csv", "2.csv")
- m = m + 1
- If Dir(myPath & c) <> "" Then
- SQL = "select * from " & c
- Set rs = cnn.Execute(SQL)
- With Sheets(m)
- .Cells.ClearContents
- For i = 1 To rs.Fields.Count
- .Cells(1, i) = rs.Fields(i - 1).Name
- Next
- .[a2].CopyFromRecordset rs
- End With
- End If
- Next
- rs.Close
- cnn.Close
- Set rs = Nothing
- Set cnn = Nothing
- End Sub
复制代码 |
|