VBA将Excel电子表格链接到Access

VBA将Excel电子表格链接到Access,vba,excel,Vba,Excel,我正在Access 2010中的VBA中创建一个代码,用于链接excel工作表并将其放入Access中的表中。在strFile=Dir(StrPath&“*.xls”)中,我不断得到一个无效的过程外路径,它不断告诉StrPath是无效的过程外路径 请帮忙 Option Compare Database Option Explicit 'code will link to excel and pull site survey files into access tables 'Setting

我正在Access 2010中的VBA中创建一个代码,用于链接excel工作表并将其放入Access中的表中。在
strFile=Dir(StrPath&“*.xls”)
中,我不断得到一个无效的过程外路径,它不断告诉
StrPath是无效的过程外路径

请帮忙

Option Compare Database
Option Explicit

'code will link to excel and pull site survey files into access tables

'Setting the path for the directory

Const strPath As String = "C:\Users\cparson\Documents\Survey_Eqpm\SiteSurveyData.xlsx"

'FileName
Dim strFile As String
'Array
Dim strFileList() As String
'File Number
Dim intFile As Integer

'Looping through the folder and building the file list
strFile = Dir(strPath & "*.xls")
While strFile <> ""
    'adding files to the list
    intFile = intFile + 1
    ReDim Preserve strFileList(1 To intFile)
    strFileList(intFile) = strFile
    strFile = Dir()
Wend
'checking to see if files where found
If intFile = 0 Then
    MsgBox "No Files Found"
    Exit Sub
End If
'going through the files and linking them to access
For intFile = 1 To UBound(strFileList)
    DoCmd.TransferSpreadsheet acLink, , _
    strFileList(intFile), strPath & strFileList(intFile), True, "A5:J17"
Next
MsgBox UBound(strFileList) & "Files were linked"
End Sub
选项比较数据库
选项显式
'代码将链接到excel并将现场调查文件拉入access表中
'设置目录的路径
Const strPath As String=“C:\Users\cparson\Documents\Survey\u Eqpm\SiteSurveyData.xlsx”
'文件名
作为字符串的Dim strFile
'阵列
Dim strFileList()作为字符串
'文件号
将文件设置为整数
'循环遍历文件夹并生成文件列表
strFile=Dir(strPath&“*.xls”)
而strFile“”
'将文件添加到列表中
intFile=intFile+1
ReDim保留strFileList(1到intFile)
strFileList(intFile)=strFile
strFile=Dir()
温德
'检查是否找到文件
如果intFile=0,则
MsgBox“未找到任何文件”
出口接头
如果结束
'浏览文件并将其链接到access
对于intFile=1到UBound(strFileList)
DoCmd.transferAcLink_
strFileList(intFile)、strPath和strFileList(intFile)、True,“A5:J17”
下一个
MsgBox UBound(strFileList)和“文件已链接”
端接头

您有一个
结束子节点
,但没有过程名称

Option Compare Database
Option Explicit

Const strPath As String = "C:\Users\cparson\Documents\Survey_Eqpm\SiteSurveyData.xlsx"

Dim strFile As String
Dim strFileList() As String
Dim intFile As Integer

Sub Sample() '<~~ You are missing this...
    strFile = Dir(strPath & "*.xls")

    '~~> Rest of your code
End Sub
选项比较数据库
选项显式
Const strPath As String=“C:\Users\cparson\Documents\Survey\u Eqpm\SiteSurveyData.xlsx”
作为字符串的Dim strFile
Dim strFileList()作为字符串
将文件设置为整数

Sub-Sample()”您也可以尝试ADO,在我的选项中这是一种简单的方法

YourConnObj.execute "SELECT * INTO  YourTableName from [Excel 14.0;DATABASE=c:\temp\data copy.xlsx].[Sheet1]"

我知道这是一个老问题,但我在谷歌搜索中发现了它,并意识到在
strPath
变量中已经有了
.xlsx
扩展名,但您也将其添加到字符串变量
strFile

Const strPath As String = "C:\Users\cparson\Documents\Survey_Eqpm\SiteSurveyData.xlsx"

strFile = Dir(strPath & "*.xls")
我可能错了,但我只是想指出这一点