简体   繁体   English

VBA 将文件夹中的所有文件导入到 Access

[英]VBA import all files in folder to Access

the goal is to import all excel files in one folder to seperate sheets in access.目标是将所有 excel 文件导入一个文件夹中以单独访问工作表。 I found this script and didnt know where to ask instead我找到了这个脚本,但不知道该问哪里

Dim blnHasFieldNames as Boolean
Dim strWorksheet As String, strTable As String
Dim strPath As String, strPathFile As String

' 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 worksheetname with the real name of the worksheet that is to be
' imported from each file
strWorksheet = "worksheetname"

' Import the data from each workbook file in the folder
strFile = Dir(strPath & "*.xls")
Do While Len(strFile) > 0
      strPathFile = strPath & strFile
      strTable = "tbl_" & Left(strFile, InStrRev(strFile, ".xls") - 1)

      DoCmd.TransferSpreadsheet acImport, _
            acSpreadsheetTypeExcel9, strTable, strPathFile, _
            blnHasFieldNames, strWorksheet & "$"

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

      strFile = Dir()
Loop 

here: http://www.accessmvp.com/KDSnell/EXCEL_Import.htm#ImpWktFilesSepTbls在这里: http : //www.accessmvp.com/KDSnell/EXCEL_Import.htm#ImpWktFilesSepTbls

I dont get how to use the code(turn it into a macro?) also is it possible to grab the 3rd row eg and take it as header ?我不明白如何使用代码(把它变成一个宏?)还有可能抓住第三行,例如,把它作为标题吗? Do you know where I can finde information about it ?你知道我在哪里可以找到有关它的信息吗?

Thanks谢谢

You could export data from Excel to Access, and start on a specific row.您可以将数据从 Excel 导出到 Access,并从特定行开始。

Sub ADOFromExcelToAccess()
' exports data from the active worksheet to a table in an Access database
' this procedure must be edited before use
Dim cn As ADODB.Connection, rs As ADODB.Recordset, r As Long
    ' connect to the Access database
    Set cn = New ADODB.Connection
    cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; " & _
        "Data Source=C:\FolderName\DataBaseName.mdb;"
    ' open a recordset
    Set rs = New ADODB.Recordset
    rs.Open "TableName", cn, adOpenKeyset, adLockOptimistic, adCmdTable  
    ' all records in a table
    r = 3 ' the start row in the worksheet
    Do While Len(Range("A" & r).Formula) > 0 
    ' repeat until first empty cell in column A
        With rs
            .AddNew ' create a new record
            ' add values to each field in the record
            .Fields("FieldName1") = Range("A" & r).Value
            .Fields("FieldName2") = Range("B" & r).Value
            .Fields("FieldNameN") = Range("C" & r).Value
            ' add more fields if necessary...
            .Update ' stores the new record
        End With
        r = r + 1 ' next row
    Loop
    rs.Close
    Set rs = Nothing
    cn.Close
    Set cn = Nothing
End Sub

Or, import everything like you are doing now, and run a small delete query on the table after doing the import.或者,像现在一样导入所有内容,并在导入后对表运行一个小的删除查询。

Something like... Delete * from Table Where Field1 is "" , or whatever.类似于... Delete * from Table Where Field1 is "" ,或其他。

The next effect is exactly the same.接下来的效果完全一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM