Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.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/0/vba/14.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文件_Excel_Vba - Fatal编程技术网

Excel VBA:如何打开最新的Excel文件

Excel VBA:如何打开最新的Excel文件,excel,vba,Excel,Vba,我下载指定路径上的文件,但每次,为了打开最近的文件,我必须将最近的文件重命名为我在宏中设置的指定名称 只要文件的路径不变,我就希望宏打开最近的文件。例如,给定两个文件my_file_fb和my_file_fb1,我想打开最近下载的文件。根据我的评论,此函数应实现以下功能: Public Function MostRecentFile(ByVal searchDirectory As String, ByVal wildCard As String) As String '''Return

我下载指定路径上的文件,但每次,为了打开最近的文件,我必须将最近的文件重命名为我在宏中设置的指定名称


只要文件的路径不变,我就希望宏打开最近的文件。例如,给定两个文件my_file_fb和my_file_fb1,我想打开最近下载的文件。

根据我的评论,此函数应实现以下功能:

Public Function MostRecentFile(ByVal searchDirectory As String, ByVal wildCard As String) As String
    '''Returns the most recent file in searchDirectory, which matches wildCard criteria
    Dim strFile As String                        'holds the name of the file we're currently looking at
    Dim mostRecent As Date                       'holds the date of creation for the most recent file
    Dim currDate As Date                         'date of creation for the current file
    Dim mostRecentPath As String                 'path of file with most recent date

    strFile = Dir(searchDirectory & wildCard)    'look for file in directory which matches wildcard
    Do While Len(strFile) > 0                    'loop until Dir returns empty quotes (no files)
        currDate = FileDateTime(searchDirectory & strFile)
        If currDate > mostRecent Then            'check whether current file is more recent than previous files
            mostRecent = currDate                'if so, update most recent date and file
            mostRecentPath = searchDirectory & strFile
        End If
        strFile = Dir                            'move to next file in directory
    Loop

    If mostRecent = 0 Then                       'check whether any files were returned
        MostRecentFile = "No files match '" & searchDirectory & wildCard & "'"
    Else
        MostRecentFile = mostRecentPath
    End If
End Function
它使用输入字符串searchDirectory和通配符,第一个指定要查找的文件夹,第二个指定要搜索的文件类型

e、 g

以字符串形式返回下载文件夹中.xlsm、.xls、.xlsx excel文件中最新文件的路径


我添加了代码注释,希望您能够了解每一步都在做什么

好的,您可以使用它来获取文件的创建日期,并使用通配符循环文件。然后比较日期,找出最近的。你能写下代码吗?也许是一个例子。那么到目前为止你都试过什么?您使用什么代码来比较返回的日期或打开文件?所以这里并不是真正的代码编写服务,所以最好展示一下您到目前为止所做的工作。试着用我建议的链接写些东西,并解释一下你到底在哪里卡住了。我对VBA真的很陌生,所以我只知道如何从特定的目的地打开特定的文件。但是,是的,我会浏览链接并尝试解决一些问题。非常感谢。我要试一试。顺便说一下,我试着使用Dir函数。代码运行时没有错误,但是,它不会在消息框中返回任何文件名。下面是语法。excel_file=DirMacintosh HD:Users:kaleembukhari:Documents,MacIDSting.xls:@kaleembukhari-Ahh,这种方法不适用于mac,因为DIR不适用于mac上的通配符。你必须在网站上搜索mac的答案。也许会有帮助。但在你的问题中,你必须始终使用标签或等效物,因为这在许多答案中都是至关重要的信息
MostRecentFile("C:/Users/[USERNAME]/Downloads/", "*.xls")