【问题标题】:Import One Worksheet from Multiple Excel Files into Multiple Access tables将一个工作表从多个 Excel 文件导入到 Multiple Access 表中
【发布时间】:2013-11-04 17:44:45
【问题描述】:

我有大约 200 个 Excel 文件,我想将它们导入到单个 Access 数据库中,并且每个文件都有一个表。每个 Excel 文件都有多个工作表,但我要导入的工作表是一致的。

我为此找到了一些代码,请参阅:http://www.accessmvp.com/KDSnell/EXCEL_Import.htm#ImpBrsFldFiles, http://social.msdn.microsoft.com/Forums/en-US/dfea25ab-cd49-495c-8096-e3a7a1484f65/importing-multiple-excel-files-with-different-file-name-into-access-using-vba

这是我尝试过的一段代码:

Option Compare Database

Sub ImportFromExcel()

End Sub

Dim strPathFile As String, strFile As String, strPath As String
Dim strTable As String, strBrowseMsg As String
Dim blnHasFieldNames As Boolean
    ' Change this next line to True if the first row in EXCEL worksheet
    ' has field names
blnHasFieldNames = True

strBrowseMsg = "C:\Users\fratep\Desktop\Long-term EWM Study Data Files\"
strPath = BrowseFolder(strBrowseMsg)
If strPath = "" Then
   MsgBox "No folder was selected.", vbOK, "No Selection"
   Exit Sub
End If

   ' Replace tablename with the real name of the table into which
   ' the data are to be imported
strTable = "tablename"

strFile = Dir(strPath & "\*.xls")
  Do While Len(strFile) > 0
    strPathFile = strPath & "\" & strFile
    DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
        strTable, strPathFile, blnHasFieldNames

 ' Uncomment out the next code step if you want to delete the
 ' EXCEL file after it's been imported
 '       Kill strPathFile

   strFile = Dir()
 Loop



Sub ImportMultiExcels()

End Sub

来自上面的第一个链接,但我似乎无法让他们做我正在寻找的事情。谁能帮帮我?

我是 VBA 新手,所以对编辑代码有点不确定。

【问题讨论】:

  • 具体是什么问题?您可以将单个工作表导入 Access 但不能将其扩展到 200 个工作表吗?还是什么?
  • 嗨,Hans,是的,据我所知,导入向导一次只能处理一张。就像我说的那样,我找到了执行该功能的 VBA 脚本,但由于某种原因,我无法让它们工作。
  • 好的,然后向我们展示您的 VBA,它成功导入了单个工作表,并描述了您在尝试将其扩展到 200 个工作表时遇到的问题。
  • 另外,我应该补充一点,我对 VBA 还是很陌生,所以我在阅读和根据我的情况定制它时遇到了一些麻烦。我确实更改了文件中的路径名,所以不是这样。
  • 抱歉,没有使用 VBA 导入单张工作表,我使用的是导入向导。这是我的问题吗?

标签: excel ms-access ms-access-2007 vba


【解决方案1】:

您似乎可以使用导入向导将工作表成功导入 Access。在这种情况下,您应该能够使用 DoCmd.TransferSpreadsheet Method 在 Access 数据库中的 VBA 代码中执行相同的操作。

以下过程将名为 XYZ Priority 的单个工作表导入为名为 Import1 的 Access 表。

我使用一个常量作为工作表名称,因为您说目标工作表在所有源工作簿文件中具有相同的名称。

我将表名构造为 "Import" 加上 i。当您将此扩展到多个工作簿时,您可以在每次导入后增加 i。或者您可能对表名有不同的策略;你没说。

我将TransferSpreadsheet 语句拆分为几行,并包含选项名称以(希望)使其更易于理解。

我的工作表包含列名,所以我有HasFieldNames:=True

我的工作簿是使用旧版本的 Excel 创建的。 SpreadsheetType:=acSpreadsheetTypeExcel9 与此一起使用; SpreadsheetType 可能需要不同的值

Public Sub Demo_TransferSpreadsheet()
    Const cstrSheetName As String = "XYZ Priority"
    Dim i As Long
    Dim strFileName As String
    Dim strTableName As String

    ' my workbook is located in the same folder as the Access db file
    strFileName = CurrentProject.Path & Chr(92) & "temp.xls"
    i = 1
    strTableName = "Import" & CStr(i)

    DoCmd.TransferSpreadsheet _
        TransferType:=acImport, _
        SpreadsheetType:=acSpreadsheetTypeExcel9, _
        Tablename:=strTableName, _
        FileName:=strFileName, _
        HasFieldNames:=True, _
        range:=cstrSheetName & "$"
End Sub

【讨论】:

  • 那么我该如何修改这段代码来为指定文件夹中的每个excel文件执行此操作?
  • 请耐心等待我。如何指定通向 CurrentProject 的路径?抱歉问了这么一个愚蠢的问题,就像我说的,我是一个全新的人。
  • 我的工作簿与包含该 VBA 过程的 Access db 文件位于同一文件夹中。所以我用CurrentProject.Path & Chr(92) 代替了实际的文件夹路径:C:\share\Access\ 在你的代码版本中,使用你喜欢的任何方法来指定文件夹。
  • 好的,再次抱歉,当我尝试运行代码时,它告诉我该项目中的宏已禁用。我试图启用它们,但我不断收到错误消息。我也关闭并重新打开了包裹,但仍然发生同样的事情。有什么想法吗?
  • 我刚刚更新了答案……“Access 数据库中的 VBA 代码”。您是否尝试改用 Excel 工作簿中的 VBA?
猜你喜欢
  • 2021-11-05
  • 1970-01-01
  • 1970-01-01
  • 2021-01-27
  • 2023-03-21
  • 1970-01-01
  • 2021-02-01
  • 2016-04-17
  • 2016-02-28
相关资源
最近更新 更多