我对Access& amp; VBA。我需要将excel文件数据导入访问表。 我试图使用以下代码来完成它。
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "TableName", importFilePath, True
在我的输入文件中有一些格式,我需要从第8行获取数据。
所以,我陷入了困境。如何从第8列读取数据。 我也指定了范围但是它产生了错误。 有人可以提供帮助吗?
答案 0 :(得分: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