|
其实用access的导入外部数据命令比代码方便多了,为什么一定要用代码呢?下面这段代码是把sheet2中的数据整体一次性导入access数据库的表“住院补偿”的命令,因为你没有附件,就这样传给你,你自己复制粘贴后就可以用(自己修改表名)
Sub 导入数据()
Sheets("sheet2").Activate
If Len([a2]) = 0 Then
MsgBox "你的信息表中没有数据,如何导入?请先汇总数据!"
Exit Sub
End If
Dim endroww%, roww%, addr$, sql$
Start = Timer
Set conn = CreateObject("ADODB.Connection")
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & ThisWorkbook.Path & _
"\db1.mdb;" & ";jet oledb:"
endroww = [a65536].End(xlUp).Row
addr = Range(Cells(1, "a"), Cells(endroww, [IV1].End(xlToLeft).Column)).Address(0, 0)
sql = "INSERT INTO [住院补偿] SELECT * FROM [Excel 8.0;Database=" & ThisWorkbook.FullName & ";HDR=YES].[sheet2$" & addr & "];"
conn.Execute sql
conn.Close
Set conn = Nothing
MsgBox "数据导入成功。用时 " & Timer - Start & " 秒"
End Sub |
|