【发布时间】:2018-02-28 19:29:20
【问题描述】:
我对 VBA 很陌生,但我正在努力学习更多。现在我正在尝试编写一个宏来打开我的 :Z 驱动器中的最新文件,该文件是一个逗号分隔文件 (.CSV)。下面的代码不起作用,但我想知道是否有人有任何建议? 谢谢你的帮助!
Sub NewestFile()
Dim MyPath As String
Dim MyFile As String
Dim LatestFile As String
Dim LatestDate As Date
Dim LMD As Date
MyPath = "Z:\"
If Right(MyPath, 1) <> “ \ ” Then MyPath = MyPath & “ \ ”
MyFile = Dir(MyPath & “ * .csv”, vbNormal)
If Len(MyFile) = 0 Then
MsgBox “No files were found…”, vbExclamation
Exit Sub
End If
Do While Len(MyFile) > 0
LMD = FileDateTime(MyPath & MyFile)
If LMD > LatestDate Then
LatestFile = MyFile
LatestDate = LMD
End If
MyFile = Dir
Loop
Workbooks.Open MyPath & LatestFile
End Sub
【问题讨论】:
-
QHarr 是的,最后修改的文件,只要是 .csv 文件就可以了
标签: vba