用路径和文件名存储在字符串变量中打开工作簿?

时间:2018-12-14 08:56:46

标签: excel vba excel-vba

Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim strPath As String
Dim strName As String

strPath = Environ("USERPROFILE") & "\Desktop\REQUIRED FILES\ART REPORT"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strPath)

For Each objFile In objFolder.Files
If objFile.DateLastModified > varDate Then
strName = objFile.Name
End If
Next

Application.Workbooks strPath.strName.Open, vbNormalNoFocus

上面的代码是检查文件夹(strPath)中是否保存了最新文件,将文件名存储在strName中,然后打开该工作簿。

我似乎找不到使用变量引用该文件的方式。

任何帮助将不胜感激。 谢谢

编辑:在以下链接中引用答案并不能解决我的问题。我不断收到错误1004 Other Question

1 个答案:

答案 0 :(得分:0)

替换最后一行 Application.Workbooks strPath.strName.Open, vbNormalNoFocus

与此一起

Application.Workbooks.Open(strPath & "\" & strName)

要获得更多选项,可以通过Workbook.Open打开,请选中MSDN docs

如果您有多个文件objFile.DateLastModified > varDate,该怎么办?现在,您将打开循环中的最后一个