简体   繁体   中英

Import data from an Excel file to an Access table

I'm trying to import the data from an Excel file selected by the user and importing it's data into a table in access.

To ask the user to select the file I use this code

Private Function importarExcelTabla()
Dim excelMedi As Variant
Dim cuadroSeleccion As Office.FileDialog

Set cuadroSeleccion = Application.FileDialog(msoFileDialogFilePicker)
'Abre el cuadro de seleccion de ficheros

With cuadroSeleccion
.AllowMultiSelect = False
.Title = "Selecciona el archivo por favor"
.Filters.Clear
.Filters.Add "Todos los archivos", "*.*", 1


If .Show = True Then
excelMedi = cuadroSeleccion.SelectedItems(1)

Once is selected I use the transgerSpreadsheet to import the .xlsx file to the table from a range

DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "MediPrueba", 
excelMedi, False, "A2:L950"

   End If

  End With

End Function

But my problem is that the table is not filled with the excel data and also I put the range from one file but:

¿It is possible to select all the document without the first line so this will work in other excel files with others lengths?

Thank you in advance

There is in fact Check out this code from www.accessmvp.com/KDSnell/EXCEL_Import.htm

This code works by selecting a starting point (top right corner) and works until it encounters a blank row at which point it stops. To skip the first row set the starting point to A2


Dim lngColumn As Long
Dim xlx As Object, xlw As Object, xls As Object, xlc As Object
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim blnEXCEL As Boolean
blnEXCEL = False

' Establish an EXCEL application object
On Error Resume Next
Set xlx = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
      Set xlx = CreateObject("Excel.Application")
      blnEXCEL = True
End If
Err.Clear
On Error GoTo 0

' Change True to False if you do not want the workbook to be
' visible when the code is running
xlx.Visible = True

' Replace C:\Filename.xls with the actual path and filename
' of the EXCEL file from which you will read the data
Set xlw = xlx.Workbooks.Open("C:\Filename.xls", , True) ' opens in read-only mode

' Replace WorksheetName with the actual name of the worksheet
' in the EXCEL file
Set xls = xlw.Worksheets("WorksheetName")

' Replace A1 with the cell reference from which the first data value
' (non-header information) is to be read
Set xlc = xls.Range("A1") ' this is the first cell that contains data

Set dbs = CurrentDb()

' Replace QueryOrTableName with the real name of the table or query
' that is to receive the data from the worksheet
Set rst = dbs.OpenRecordset("QueryOrTableName", dbOpenDynaset, dbAppendOnly)

' write data to the recordset
Do While xlc.Value <> ""
      rst.AddNew
            For lngColumn = 0 To rst.Fields.Count - 1
                  rst.Fields(lngColumn).Value = xlc.Offset(0, lngColumn).Value
            Next lngColumn
      rst.Update
      Set xlc = xlc.Offset(1,0)
Loop

rst.Close
Set rst = Nothing

dbs.Close
Set dbs = Nothing

' Close the EXCEL file without saving the file, and clean up the EXCEL objects
Set xlc = Nothing
Set xls = Nothing
xlw.Close False
Set xlw = Nothing
If blnEXCEL = True Then xlx.Quit
Set xlx = Nothing

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM