我需要创建一个宏来打开另一个excel文件(所以我可以复制内容)。这个文件定期刷新,我需要从该文件的最新版本中获取数据。每个文件的名称都有变化,因为最新版本的版本号较大(不是增量版)。他们还有一个我需要考虑的日期。
以下是文件的示例。
2016年3月16日下午4点8分
"每日全球模型Extract_GL_FM_2016_03_16_165819.xls"
2016年3月16日下午5点58分保存
"每日全球模型Extract_GL_FM_2016_03_16_174817.xls"
我需要一些能够处理文档名称更改的强大功能。
答案 0 :(得分:0)
试一试。
{ ...['a', 'b', 'c'] }
答案 1 :(得分:0)
谢谢Zach,我发现这似乎很有效
'强制显式删除变量 选项明确
Sub OpenLatestFile()
'Declare the variables
Dim MyPath As String
Dim MyFile As String
Dim LatestFile As String
Dim LatestDate As Date
Dim LMD As Date
'Specify the path to the folder
MyPath = "C:\Users\Domenic\Documents\"
'Make sure that the path ends in a backslash
If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
'Get the first Excel file from the folder
MyFile = Dir(MyPath & "*.xls", vbNormal)
'If no files were found, exit the sub
If Len(MyFile) = 0 Then
MsgBox "No files were found...", vbExclamation
Exit Sub
End If
'Loop through each Excel file in the folder
Do While Len(MyFile) > 0
'Assign the date/time of the current file to a variable
LMD = FileDateTime(MyPath & MyFile)
'If the date/time of the current file is greater than the latest
'recorded date, assign its filename and date/time to variables
If LMD > LatestDate Then
LatestFile = MyFile
LatestDate = LMD
End If
'Get the next Excel file from the folder
MyFile = Dir
Loop
'Open the latest file
Workbooks.Open MyPath & LatestFile
End Sub