【发布时间】:2014-02-05 13:20:50
【问题描述】:
我想将某个目录中的所有 Excel 文件(具有不同的数据和列)导入 MS Access 2010 数据库,为每个文件创建新表。我找到了将文件导入一个表的代码:
Option Compare Database
Option Explicit
Function DoImport()
Dim strPathFile As String, strFile As String, strPath As String
Dim strTable As String
Dim blnHasFieldNames As Boolean
' Change this next line to True if the first row in EXCEL worksheet
' has field names
blnHasFieldNames = True
' Replace C:\Documents\ with the real path to the folder that
' contains the EXCEL files
strPath = "C:\Documents and Settings\myName\My Documents\Access Test\"
' 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
End Function
但我每次都需要创建新表。在 VBA 中可以吗?
【问题讨论】:
-
re: “如果有人能提供有关创建按钮、模块和运行的逐步示例,我将非常感激。” - 哇。如果那不是“太宽泛”,我不知道 是什么。
-
我在网上冲浪的时间已经够久了,但还没有找到关于如何创建按钮并将其与模块链接的示例。我相信很快就会给出一个快速指南。无论如何,我的主要问题是在循环中创建新表。
-
您希望如何命名新表?
-
@HansUp,根据文件名。
标签: ms-access vba ms-access-2010