2013-10-21 141 views
-1

我對VBA Access &完全陌生。我需要將excel文件數據導入訪問表。 我正在嘗試使用下面的代碼。將Excel文件數據導入Access數據庫VBA

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

在我的輸入文件中有一些格式,我需要從第8行獲取數據。

所以,我被困在這一點。如何從第8列讀取數據。 我也指定了範圍,但然後它產生了錯誤。 任何人都可以幫忙嗎?

+0

可能重複[通過VBA將MS Access表導出到同一目錄中的Excel電子表格](http://stackoverflow.com/questions/1849580/export-ms-access-tables-through-vba-to-an-excel-spreadsheet-in -same-directory) – 2013-10-21 08:40:15

+0

你想要第8列還是第8列的數據? – HansUp

回答

0

當然,這樣做是創建在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 
相關問題