|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
以下这段代码 可以将xls文件导入access,现在想用来导入csv文件 请教高手 ,该如何修改代码?谢谢!!!
Sub jb0704() '导入ACCESS汇总
Dim filename, fn As Variant
Dim sh As Worksheet
Application.ScreenUpdating = False
filename = Application.GetOpenFilename("所有文件(*.xls),*.xls", , , , True) '选取一个范围,可以选多个excel文件
If Not IsArray(filename) Then Exit Sub
myname = InputBox("请输入表名......", "温馨提示", "首次使用请在此处给表命名!")
Set objAccess = CreateObject("Access.Application")
With objAccess
.OpenCurrentDatabase ("e:\数据资料\统计单.mdb")
.DoCmd.TransferSpreadsheet acImport, 8, myname, filename(1), True, "a1:iv1" '导入第1行表头
Set conn = CreateObject("adodb.connection")
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=e:\数据资料\统计单.mdb"
For Each fn In filename
fname = Dir(fn)
Workbooks.Open fname
For Each sh In ActiveWorkbook.Sheets
conn.Execute "INSERT INTO [" & myname & "] SELECT * FROM [Excel 8.0;DATABASE=" & fn & ";].[" & sh.Name & "$];"
Next
Workbooks(fname).Close False
Next
MsgBox "数据导入完毕!", 64, "提示"
End With
conn.Close
Set conn = Nothing
Application.ScreenUpdating = True
End Sub
[ 本帖最后由 hangye 于 2010-9-3 13:47 编辑 ] |
|