Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.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 - Fatal编程技术网

Excel VBA下标超出工作簿名称的范围

Excel VBA下标超出工作簿名称的范围,excel,vba,Excel,Vba,我从网上提取了一些代码,在文件夹中打开了最新的文件,看起来效果不错。然而,在代码的后面,我添加了一行来设置最近打开的同一个文件。尝试此操作时工作簿子原则超出范围,我认为这与语法有关?可能需要在工作簿名称中加入其他引号,有什么想法吗 “path”和“latestFile”变量似乎正在正确读入 Dim path$, file$, latestFile$ Dim LatestDate As Date, LMD As Date Dim D As worksheet, dash As worksheet

我从网上提取了一些代码,在文件夹中打开了最新的文件,看起来效果不错。然而,在代码的后面,我添加了一行来设置最近打开的同一个文件。尝试此操作时工作簿子原则超出范围,我认为这与语法有关?可能需要在工作簿名称中加入其他引号,有什么想法吗

“path”和“latestFile”变量似乎正在正确读入

Dim path$, file$, latestFile$
Dim LatestDate As Date, LMD As Date
Dim D As worksheet, dash As worksheet

'open latest file
path = "R:\Dept\"
If Right(path, 1) <> "\" Then path = path & "\"

file = Dir(path & "*.xls", vbNormal)

If Len(file) = 0 Then
    MsgBox "No files were found in the folder", vbExclamation
End If

Do While Len(file) > 0
    LMD = FileDateTime(path & file)
    If LMD > LatestDate Then
        latestFile = file
        LatestDate = LMD
    End If
    file = Dir
Loop


Application.DisplayAlerts = False
Workbooks.Open path & latestFile
Application.DisplayAlerts = True

Set dash = Workbooks("dashboard.xlsm").Worksheets("D data")
Set D = Workbooks(path & latestFile).Worksheets("D Data") 'error here
Dim路径$,文件$,最新文件$
Dim LatestDate作为日期,LMD作为日期
将D标注为工作表,虚线标注为工作表
'打开最新文件
path=“R:\Dept\”
如果正确(路径,1)“\”则路径=路径&“\”
file=Dir(路径&“*.xls”,vbNormal)
如果Len(file)=0,则
MsgBox“在文件夹中找不到任何文件”,请使用感叹号
如果结束
当Len(文件)>0时执行
LMD=FileDateTime(路径和文件)
如果LMD>最晚日期,则
最新文件=文件
LatestDate=LMD
如果结束
file=Dir
环
Application.DisplayAlerts=False
工作簿。打开路径和最新文件
Application.DisplayAlerts=True
Set dash=工作簿(“dashboard.xlsm”).工作表(“D数据”)
设置D=工作簿(路径和最新文件)。此处的工作表(“D数据”)错误
只需使用名称:

Set D = Workbooks(latestFile).Worksheets("D Data")
但要做到这一点:

Application.DisplayAlerts = False
Set D = Workbooks.Open(path & latestFile).Worksheets("D Data") '<< edit
Application.DisplayAlerts = True
Application.DisplayAlerts=False

Set D=Workbooks.Open(path&latestFile).Worksheets(“D数据”)“哦,是的,我以前见过这样写的,非常干净-但现在我遇到了一个“类型不匹配”错误?”“path”和“latestFile”都是字符串,这是洁食吗?实际上你的第一个建议解决了我的问题,所以我会用它,谢谢!