Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.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
获取文件夹中所有文件的VBA代码找不到文件_Vba_Ms Access - Fatal编程技术网

获取文件夹中所有文件的VBA代码找不到文件

获取文件夹中所有文件的VBA代码找不到文件,vba,ms-access,Vba,Ms Access,我试图设置一个宏,将文件夹中的所有excel文件拉入access中的数据库。我有下面的代码,但当我运行宏时,它会出错为“找不到文件”,因此intFile=0。但是,所选文件夹中有文件。为什么找不到他们?我想我也弄糟了连接件,但一次只出一个问题。我显然是VBA的新手,所以任何帮助都将不胜感激! 谢谢 选项比较数据库 选项显式 '代码将链接到excel并将现场调查文件拉入access表中 '设置目录的路径 Const strPath As String=“S:\LOG\PURCHASI\Daniel

我试图设置一个宏,将文件夹中的所有excel文件拉入access中的数据库。我有下面的代码,但当我运行宏时,它会出错为“找不到文件”,因此intFile=0。但是,所选文件夹中有文件。为什么找不到他们?我想我也弄糟了连接件,但一次只出一个问题。我显然是VBA的新手,所以任何帮助都将不胜感激! 谢谢

选项比较数据库
选项显式
'代码将链接到excel并将现场调查文件拉入access表中
'设置目录的路径
Const strPath As String=“S:\LOG\PURCHASI\Daniel Binkoski\Outlook Attachments\R7398Z向前看每日快照”
'文件名
作为字符串的Dim strFile
'阵列
Dim strFileList()作为字符串
'文件号
将文件设置为整数
子样本()
strFile=Dir(strPath&“*.xlsx”)
'循环遍历文件夹并生成文件列表
strFile=Dir(strPath&“*.xlsx”)
而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,“A1:M50”
下一个
MsgBox UBound(strFileList)和“文件已链接”
端接头
试试:

或者在strPath值上添加最后一个“\”

您需要另一个路径分隔符来显示您正在查找的是一个目录,而不是一个目录

我经常使用类似于:

Dir(strPath & IIf(Right(strPath, 1) = "\", vbNullString, "\"))
作为确保路径始终以尾随反斜杠结束的检查

strFile = Dir(strPath & "\*.xlsx", vbNormal)
Dir(strPath & IIf(Right(strPath, 1) = "\", vbNullString, "\"))