Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA导入文件夹中的所有文件以访问_Excel_Vba_Ms Access 2010 - Fatal编程技术网

Excel VBA导入文件夹中的所有文件以访问

Excel VBA导入文件夹中的所有文件以访问,excel,vba,ms-access-2010,Excel,Vba,Ms Access 2010,目标是将所有excel文件导入到一个文件夹中,以在access中分隔图纸。我找到了这个脚本,不知道该去哪里问 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 '

目标是将所有excel文件导入到一个文件夹中,以在access中分隔图纸。我找到了这个脚本,不知道该去哪里问

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 
在这里:

我不知道如何使用代码(把它变成一个宏?)也有可能抓住第三行例如,并采取它作为标题?你知道我在哪里可以找到关于它的信息吗


谢谢

您可以将数据从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
或者,像现在一样导入所有内容,并在导入后对表运行一个小的删除查询

类似于…
从表中删除*,其中字段1为“”
,或其他任何内容


下一个效果完全相同。

我想将.xls文件导入access@dai,感谢heads up
DoCmd
是access中的一个对象。看看答案。它应该对你有帮助