How do I convert Excel files, then import to Access in one step? [closed]

十年热恋 提交于 2019-12-25 17:42:39

问题


I posted this question yesterday, but people advised me to ask again, with more detail.

I have several (+1000) raw files that I need to convert, and eventually import into Access

. I've written an Excel macro that will do that. It's fairly extensive, and has a great deal of operations, but the 'gist' of it is below. It asks a user to select some files, then saves the files to the same directory as the original raw files. It's called ConvertWithDialog, and in the interest of saving space, is in the Paste below.

<script src="https://pastebin.com/embed_js/2cfKLbAr"></script>

I also have a version without the dialog, which can be run by passing file(s) to it through a separate function.

<script src="https://pastebin.com/embed_js/9AB8VKgL"></script>

My issue is importing to Access. How do I convert the multiple files and import to Access in one step(one button press in Access)? I don't want to have to run the convert function, wait for the files to finish converting, then import to Access. I tried this, as suggested by a user previously, but when I added the arguments as demonstrated, VBA threw an error, saying a '=' was expected. I've a macro to batch import Access files, and can include it. but it's not very elegant, and I'm sure I'll have to do it differently in the end anyway.


回答1:


Import Data from All EXCEL Files in a single Folder via TransferSpreadsheet (VBA)

Generic code to import the data from the first (or only) worksheet in all EXCEL files that are located within a single folder. All of the EXCEL files' worksheets must have the data in the same layout and format.

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 = False

' Replace C:\Documents\ with the real path to the folder that
 ' contains the EXCEL files
 strPath = "C:\Documents\"

' 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


来源:https://stackoverflow.com/questions/44661823/how-do-i-convert-excel-files-then-import-to-access-in-one-step

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!