如何在Access VBA

时间:2017-06-29 13:43:18

标签: excel-vba ms-access access-vba copy vba

我在Excel中有一个模板,有22列,大约50行。此模板将填充字段报告数据,并且我希望将每个字段报告存储在相同类型模板(22列+ ID列)的访问数据库中。如何在Excel中使用VBA引用访问中的行和列?

例如,在excel中,为了将行从一个工作簿复制到另一个工作簿,我会这样做:

工作簿(“WorkbookName1”)。工作表(“工作表1”)。范围(“A4:V4”)。结束(xlDown)。复制_目的地:= ThisWorkbook.Worksheets(1).Range(“A4”) .Range(完(xlDown)))

1 个答案:

答案 0 :(得分:1)

Access和Excel非常不同。我想你想自动将多个Excel文件(模板)中的数据加载到Access表中。看看下面的脚本。

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

从Access运行代码。它将从文件夹中的所有Excel文件导入数据。