|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
如果csv文件比较规范,可以考虑使用ADO法:- Sub ADO()
- Dim Fso As Object, File As Object, cnn As Object, SQL$, f, p
- Application.ScreenUpdating = False
- p = ThisWorkbook.Path & ""
- Set Fso = CreateObject("Scripting.FileSystemObject")
- Set cnn = CreateObject("adodb.connection")
- cnn.Open "provider=Microsoft.jet.OLEDB.4.0;Extended Properties =Excel 8.0;Data Source=" & ThisWorkbook.FullName
- For Each File In Fso.GetFolder(ThisWorkbook.Path).Files
- If File.Name Like "*.csv" Then
- f = Replace(File, ".csv", ".xls")
- If Fso.FileExists(f) Then Fso.DeleteFile (f)
- SQL = "SELECT * INTO [Excel 8.0;Database=" & f & ";]." & Replace(File.Name, ".csv", "") & " FROM [Text;FMT=Delimited;HDR=Yes;DATABASE=" & p & ";]." & File.Name
- cnn.Execute SQL
- End If
- Next
- Set File = Nothing
- Set Fso = Nothing
- cnn.Close
- Set cnn = Nothing
- Application.ScreenUpdating = True
- End Sub
复制代码 |
|