在以下代码中,我可以将1行数据从Excel导出到Acces数据库。但是我该如何更改代码以导出更多行,例如A列中的值不为空的所有行? (还有更简单的代码吗?)
Sub TestAccess()
Dim DONNEE_1 As String
Dim DONNEE_2 As String
Dim DONNEE_3 As String
Dim DONNEE_4 As String
Dim WBK1 As Workbook
Dim WST1 As Worksheet
Set WBK1 = ActiveWorkbook
Set WST1 = WBK1.Worksheets("Pays")
我导出的第一行数据
DONNEE_1 = CStr(WST1.Range("A2"))
DONNEE_2 = CStr(WST1.Range("B2"))
DONNEE_3 = CStr(WST1.Range("C2"))
DONNEE_4 = CDate(Now())
Set cn = CreateObject("ADODB.Connection")
我的目标Access数据库的名称:
Fichier = "V:\20-STAT-statistiques\WorkDJ\Access test\Test.accdb"
与我的Excel文件建立连接:
Set cn = New ADODB.Connection
cn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" &
Fichier & ";Persist Security Info=False"
cn.Open
我在Acces数据库中要添加数据的列
Set oCm = New ADODB.Command
oCm.ActiveConnection = cn
oCm.CommandText = "INSERT INTO Voyages (Ville,Pays, Continent, Ajout)" &
_"VALUES ('" & DONNEE_1 & "','" & DONNEE_2 & "','" & DONNEE_3 & "', #" &
DONNEE_4 & "#)"
oCm.Execute
cn.Close
End Sub