简体   繁体   中英

Import Excel file data into Access Database VBA

I am completely new to Access & VBA. I need to import the excel file data into access table. I was trying to do it using following code.

DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "TableName", importFilePath, True

In my input file there is some format and I need to get the data from 8th line.

So, I am stuck at this point. How to read the data from 8th column. I have also specified range but then it generated the error. Can anybody help on this?

Certainly one way to do it would be to create a temporary copy of the file, open that copy in Excel, delete the first 7 rows, save it, and then import that copy:

Sub ExcelImportTest()
    Dim fso As Object  ' FileSystemObject
    Dim f As Object  ' File
    Dim strTempPath As String
    Dim objExcel As Object  ' Excel.Application
    Dim objWorkbook As Object  ' Excel.Workbook
    Const TemporaryFolder = 2

    Set fso = CreateObject("Scripting.FileSystemObject")  ' New FileSystemObject
    strTempPath = fso.GetSpecialFolder(TemporaryFolder) & "\" & fso.GetTempName & "\"
    fso.CreateFolder strTempPath
    Set f = fso.GetFile("C:\Users\Gord\Desktop\toImport.xls")
    fso.CopyFile f.Path, strTempPath & f.Name

    Set objExcel = CreateObject("Excel.Application")  ' New Excel.Application
    Set objWorkbook = objExcel.Workbooks.Open(strTempPath & f.Name)
    objWorkbook.ActiveSheet.Rows("1:7").EntireRow.Select
    objExcel.Selection.Delete
    objWorkbook.ActiveSheet.Range("A1").Select
    objWorkbook.Save
    Set objWorkbook = Nothing
    objExcel.Quit
    Set objExcel = Nothing

    DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "ExcelData", strTempPath & f.Name, True

    fso.DeleteFile strTempPath & f.Name
    fso.DeleteFolder Left(strTempPath, Len(strTempPath) - 1)

    Set f = Nothing
    Set fso = Nothing
End Sub

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