ciao Valerio,
credo sia normale ragionando con la logica di Excel ragionare riga per riga, l'approccio cambia invece se ci rapportiamo ad un database relazionale.
suppondo la tabella di destinazione come quella che segue, presa dal database di esempio NorthWind :

e questo per quanto al file di excel di origine :

prova per esempio ad eseguire le due soluzione che seguono.
Option Explicit
Private Const strPathDb As String = "C:\database.accdb" '<------personalizza
Private Function fileExists(strFullPath As String) As Boolean
On Error Resume Next
fileExists = ((GetAttr(strFullPath) And vbDirectory) = 0)
End Function
Public Sub exportTOAccess3()
If Not fileExists(strPathDb) Then Exit Sub
On Error GoTo errorHandler
Dim strSql As String
Dim wbk As Excel.Workbook
Dim wsh As Excel.Worksheet
Dim conn As ADODB.Connection
Dim lngRecords As Long
Dim lngLastRow As Long
Dim lngLastCol As Long
Dim rngAddress As Excel.Range
Const cnn As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strPathDb
Const lngFirstRow As Long = 1
Const wshName As String = "foglio1"
Set wbk = ThisWorkbook
Set wsh = wbk.Worksheets(wshName)
With wsh
lngLastRow = .Range("A" & .Rows.Count).End(xlUp).Row
lngLastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
Set rngAddress = .Range(Cells(1, 1), Cells(lngLastRow, lngLastCol))
End With
Set conn = New ADODB.Connection
With conn
.Open cnn
strSql = "INSERT INTO OrdiniImport " _
& "SELECT * FROM [Excel 12.0;HDR=YES;DATABASE=" & wbk.FullName & "].[" & wsh.Name & "$" & Replace(rngAddress.Address, "$", "") & "]"
.Execute CommandText:=strSql, RecordsAffected:=lngRecords, Options:=-1
.Close
End With
VBA.MsgBox prompt:="Ho inserito nella tabella OrdiniImport " & lngRecords & " Righe.", _
Buttons:=vbOKOnly, _
Title:="Informazione"
exitErrorHandler:
Set wsh = Nothing
Set wbk = Nothing
Set rngAddress = Nothing
Exit Sub
errorHandler:
With Err
VBA.MsgBox "ERR#" & CStr(.Number) _
& vbNewLine & .Description _
, vbOKOnly Or vbCritical
End With
Resume exitErrorHandler
End Sub
oppure :
Public Sub AccessExecute()
On Error GoTo errorHandler
Dim app_Access As Access.Application
Dim wbk As Excel.Workbook
Dim wsh As Excel.Worksheet
Dim lngRecords As Long
Dim lngLastRow As Long
Dim lngLastCol As Long
Dim rngAddress As Excel.Range
Dim strSql As String
Dim dbs As DAO.Database
Set wbk = ActiveWorkbook
Set wsh = wbk.Worksheets(1)
With wsh
lngLastRow = .Range("A" & .Rows.Count).End(xlUp).Row
lngLastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
Set rngAddress = .Range(Cells(1, 1), Cells(lngLastRow, lngLastCol))
End With
strSql = "INSERT INTO OrdiniImport " _
& "SELECT * FROM [Excel 12.0;HDR=YES;DATABASE=" & wbk.FullName & "].[" & wsh.Name & "$" & Replace(rngAddress.Address, "$", "") & "]"
Set app_Access = Access.Application
Set dbs = app_Access.DBEngine(0).OpenDatabase(strPathDb)
With dbs
.Execute Query:=strSql, Options:=&H80
lngRecords = .RecordsAffected
End With
VBA.MsgBox prompt:="Ho inserito nella tabella OrdiniImport " & lngRecords & " Righe.", _
Buttons:=vbOKOnly, _
Title:="Informazione"
exitErrorHandler:
Set wsh = Nothing
Set wbk = Nothing
Set rngAddress = Nothing
dbs.Close
Set app_Access = Nothing
Exit Sub
errorHandler:
With Err
VBA.MsgBox "ERR#" & CStr(.Number) _
& vbNewLine & .Description _
, vbOKOnly Or vbCritical
End With
Resume exitErrorHandler
End Sub
meglio la prima.
Facci sapere.
Ciao, Sandro.