Ms access 将不同的excel文件自动导入MS Access 2010表格
我想将所有Excel文件(具有不同的数据和列)从某个目录导入MS Access 2010数据库,为每个文件创建新表。我找到了将文件导入到一个表中的代码:Ms access 将不同的excel文件自动导入MS Access 2010表格,ms-access,vba,ms-access-2010,Ms Access,Vba,Ms Access 2010,我想将所有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
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中有可能吗?我以前是MOS Access 2003。现在每个人都在使用2010,但很多事情都没有改变 手动导入或导出时,可以将布局另存为规范 此过程可以通过宏实现自动化 查看下面的链接了解更多详细信息和步骤 至于其他物品、按钮、模块等,请先阅读联机帮助/文档 我们是来帮你的,但不是来帮你的
J好的,我不知道这是否是我的电脑不在当前办公室的问题 以下是如何使用ImportExport宏的链接。用于位于宏部分 我确实读到你必须相信这个地方。因此,我尝试了我的位置c:\msdn和向导的默认位置 还是没能让它选择出来 我试图创建一个规范,看看是否需要一个规范来显示选项,没有骰子 但是,有DoCmd.TransferText和DoCmd.TransferSpreadSheet 两者都允许您导入 创建一个函数。从宏(运行代码)调用函数。另一种方法是创建主菜单窗体。有一个按钮。在click命令上,运行代码 请告诉我您是否有过要显示的ImportExportData宏。我认为这是一个错误。我需要记录最新的累积更新,然后重试
我认为每次执行
DoCmd.TransferSpreadsheet
之前,您需要做的就是更改目标表名(strTable的值)
在一条评论中,您说希望表名派生自工作簿文件名。并且,每次通过循环时,另一个变量(strFile
)包含文件名。因此,我认为您可以从该文件名中删除文件扩展名,并将其用作访问表名
下面是一个即时窗口示例,演示了如何做到这一点
strFile=“foo.xls”
strTable=左(strFile,Len(strFile)-4)
? 标准的
福
如果这种方法合适,请像下面这样修改VBA代码中的循环(未测试的)代码片段
strFile=Dir(strPath&“*.xls”)
当Len(strFile)>0时执行
strPathFile=strPath&strFile
strTable=左(strFile,Len(strFile)-4)
DoCmd.transfer电子表格导入,acSpreadsheetTypeExcel9_
strTable,strPathFile,blnHasFieldNames
strFile=Dir()
环
re:“如果有人能提供关于创建按钮、模块和运行的逐步示例,我将不胜感激。”-哇。如果这不是“太宽泛”,我不知道是什么。我上网已经够久了,但还没有找到如何创建按钮并将其与模块链接的示例。我相信很快就能给你一个快速的指导。无论如何,我的主要问题是在循环中创建新表。您希望新表的名称如何?@HansUp,根据文件名。好的,将我的问题编辑为听起来不那么恳求:)谢谢您的链接,但仍然有一种方法可以导入单个文件,覆盖相同的表。这正是我想要的,非常简单。非常感谢!我从宏调用我的函数,它工作正常,谢谢你的帮助!