简体   繁体   English

将 Excel 文件数据导入 Access 数据库 VBA

[英]Import Excel file data into Access Database VBA

I am completely new to Access & VBA.我对 Access 和 VBA 完全陌生。 I need to import the excel file data into access table.我需要将excel文件数据导入访问表。 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.在我的输入文件中有某种格式,我需要从第 8 行获取数据。

So, I am stuck at this point.所以,我被困在这一点上。 How to read the data from 8th column.如何从第 8 列读取数据。 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:当然,一种方法是创建文件的临时副本,在 Excel 中打开该副本,删除前 7 行,保存,然后导入该副本:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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