I have this code i found online to export data from Excel to Access and it works fine to an extent but i am having issues with trying to export a range of cells. I can export one cell but it doesn't see to like when try to export a range. Instead i am trying to use a loop instead to export the range but still can't get it to work.
I get the Next without for Error
I have tried this method with no luck either 'rs!Column1 = Sheets("Sheet1").Range("O2:O170")
Code below:
Sub AdddNewDatatoAccDb()
Dim cn As ADODB.Connection, rs As ADODB.Recordset
Dim i As Integer
Set cn = New ADODB.Connection
With cn
.ConnectionString = con1
.Open "T:\Folder1\VBA Test.accdb"
End With
Set rs = New ADODB.Recordset
rs.Open "VBAtest", cn, adOpenDynamic, adLockPessimistic, adCmdTable
For i = 0 To 170
With rs
.AddNew
rs!Column1 = Worksheets("Sheet1").Cells(i + 2, 0).Value
rs!Column2 = Worksheets("Sheet1").Cells(i + 2, 1).Value
Next i
End With
Set rs = Nothing
cn.Close
Set cn = Nothing
Exit Sub
End Sub
`