VBA是否使用通配符打开工作簿?

VBA是否使用通配符打开工作簿?,vba,excel,wildcard,Vba,Excel,Wildcard,我在以下目录中有一个.xlsx文件: G:\BUYING\Food Specials\2. Planning\1. Planning\1. Planner\8. 2017\2017 Planner.xlsx 我可以把它指向这个目录,但目录会根据年份的不同而变化 所以这个目录可以变成: G:\BUYING\Food Specials\2. Planning\1. Planning\1. Planner\9. 2018\2018 Planner.xlsx 要处理此问题,我尝试向路径添加通配符,如

我在以下目录中有一个.xlsx文件:

G:\BUYING\Food Specials\2. Planning\1. Planning\1. Planner\8. 2017\2017 Planner.xlsx
我可以把它指向这个目录,但目录会根据年份的不同而变化

所以这个目录可以变成:

G:\BUYING\Food Specials\2. Planning\1. Planning\1. Planner\9. 2018\2018 Planner.xlsx
要处理此问题,我尝试向路径添加通配符,如下所示:

G:\BUYING\Food Specials\2. Planning\1. Planning\1. Planner\" & "*." & " " & Year(Date) & "\"
我的工作簿将无法打开。 有人能告诉我哪里出了问题吗

分:

功能:

Function FindDepotMemo() As String

    Dim Path As String
    Dim FindFirstFile As String

    Path = "G:\BUYING\Food Specials\2. Planning\1. Planning\1. Planner\" & "*." & " " & Year(Date) & "\"

    FindFirstFile = Dir$(Path & "*.xlsx")

    While (FindFirstFile <> "")

        If InStr(FindFirstFile, "Planner") > 0 Then

            FindDepotMemo = Path & FindFirstFile
            Exit Function

        End If

        FindFirstFile = Dir

    Wend

End Function
函数FindDepotMemo()作为字符串
将路径设置为字符串
Dim FindFirstFile作为字符串
Path=“G:\BUYING\Food Specials\2.Planning\1.Planning\1.plannier\”和“*”和“&”&年份(日期)和“\”
FindFirstFile=Dir$(路径和“*.xlsx”)
而(FindFirstFile“”)
如果InStr(FindFirstFile,“Planner”)>0,则
FindDepotMemo=路径和FindFirstFile
退出功能
如果结束
FindFirstFile=Dir
温德
端函数

试试这种方法。代码循环遍历文件夹名称1。2020, 1. 2019, 1. 2018年和本年度,找到最低值,比如1。2018年存在。这一年,它会寻找最高的月份数,放弃上一个月份,寻找下一个更高的月份数。如果,比如说,4。这是它使用原始代码查找文件名的路径

Function FindDepotMemo() As String

    Dim Pn As String                                ' Path name
    Dim Fn As String                                ' Folder name
    Dim Sp() As String                              ' Split string
    Dim Arr() As String, Tmp As String
    Dim FindFirstFile As String
    Dim i As Integer
    Dim n As Integer

    For i = 2020 To Year(Date) Step -1
        Pn = "G:\BUYING\Food Specials\2. Planning\1. Planning\1. Planner\1. " & CStr(i) & "\"
        If Len(Dir(Pn, vbDirectory)) Then Exit For
    Next i

    If i >= Year(Date) Then
        Sp = Split(Pn, "\")
        Arr = Sp
        n = UBound(Sp) - 1
        Fn = Sp(n)
        For i = 2 To 12
            Arr(n) = CStr(i) & Mid(Fn, 2)
            Pn = Join(Arr, "\")
            If Len(Dir(Pn, vbDirectory)) = 0 Then Exit For
            Sp = Arr
        Next i

        FindFirstFile = Join(Sp, "\") & "*.xlsx"
        While (FindFirstFile <> "")
            If InStr(FindFirstFile, "Planner") > 0 Then
                FindDepotMemo = Pn & FindFirstFile
                Exit Do
            End If
            FindFirstFile = Dir
        Wend
    End If
End Function
函数FindDepotMemo()作为字符串
Dim Pn作为字符串的路径名
Dim Fn作为字符串“文件夹名称”
Dim Sp()作为字符串的拆分字符串
Dim Arr()作为字符串,Tmp作为字符串
Dim FindFirstFile作为字符串
作为整数的Dim i
作为整数的Dim n
对于i=2020至年份(日期)步骤-1
Pn=“G:\BUYING\Food Specials\2.Planning\1.Planning\1.plannier\1.”&CStr(i)&“\”
如果Len(Dir(Pn,vbDirectory))则退出
接下来我
如果i>=年(日期),则
Sp=拆分(Pn,“\”)
Arr=Sp
n=UBound(Sp)-1
Fn=Sp(n)
对于i=2到12
Arr(n)=CStr(i)和Mid(Fn,2)
Pn=连接(Arr,“\”)
如果Len(Dir(Pn,vbDirectory))=0,则退出
Sp=Arr
接下来我
FindFirstFile=Join(Sp,“\”&“*.xlsx”
而(FindFirstFile“”)
如果InStr(FindFirstFile,“Planner”)>0,则
FindDepotMemo=Pn&FindFirstFile
退出Do
如果结束
FindFirstFile=Dir
温德
如果结束
端函数

如果不是1。如果找到2017或更高版本,则函数返回空字符串。由于缺少数据,我无法测试代码。

有很多方法,但我会使用这种方法:

Function FindDepotMemo() As String

    Dim oldPath, tempPath, newPath As String
    Dim FindFirstFile As String
    Dim addInt as Integer

    ' ASSUMING YOU ALREADY HAVE THIS PATH
    ' WE WILL USE THIS AS BASE PATH
    ' AND WE WILL INCREMENT THE NUMBER AND YEAR IN IT
    oldPath = "G:\BUYING\Food Specials\2. Planning\1. Planning\1. Planner\8. 2017"

    ' EXTRACT 8. 2017 FROM oldPath
    tempPath = Mid(oldPath, InStrRev(oldPath, "\") + 1)

    ' GET THE YEAR DIFFERENCE
    addInt = Year(Date) - CInt(Split(tempPath, ".")(1))

    ' ADD THIS DIFFERENCE TO NUMBER AND YEAR
    ' AND NOW YOU HAVE CURRENT YEAR FOLDER
    newTemp = Split(tempPath, ".")(0) + addInt & ". " & Split(tempPath, ".")(1) + addInt

    FindFirstFile = Dir$(Path & "\" & "*.xlsx")

    While (FindFirstFile <> "")
        If InStr(FindFirstFile, "Test") > 0 Then
            FindDepotMemo = Path & FindFirstFile
            Exit Function
        End If
        FindFirstFile = Dir
    Wend

End Function
函数FindDepotMemo()作为字符串
将oldPath、tempPath、newPath设置为字符串
Dim FindFirstFile作为字符串
作为整数的Dim addInt
'假设您已经拥有此路径
'我们将使用它作为基本路径
“我们将增加它的数量和年份
oldPath=“G:\BUYING\Food Specials\2.Planning\1.Planning\1.Planner\8.2017”
"摘录8。2017年来自oldPath
tempPath=Mid(oldPath,InStrRev(oldPath,“\”)+1)
“获得年度差异
附加=年份(日期)-CInt(拆分(临时路径“.”(1))
'将此差异添加到数字和年份中
'现在您有了“当前年份”文件夹
newTemp=Split(tempPath,“.”)(0)+addInt&“&”和Split(tempPath,“.”)(1)+addInt
FindFirstFile=Dir$(路径&“\”和“*.xlsx”)
而(FindFirstFile“”)
如果InStr(FindFirstFile,“Test”)>0,则
FindDepotMemo=路径和FindFirstFile
退出功能
如果结束
FindFirstFile=Dir
温德
端函数

我已经添加了一些评论,以帮助您了解我在做什么。

这似乎是对您问题的一个公正回答。我不明白你为什么不知道你要打开的确切文件?当然,你可以从用户那里获取一个输入,比如
strYearOfInterest
,然后使用该输入设置一个变量,然后将该变量传递到文件名的字符串中?@Variatus谢谢,我尝试了这个方法,但我想这是因为我使用了带数字的通配符,而它与你上一个文件夹应该是
8的相同。2017年
?你不能把它作为2017年的唯一版本吗?因为,没有
8,一切正常。
@ManishChristian不幸的是,我无法更改文件夹:(
Function FindDepotMemo() As String

    Dim oldPath, tempPath, newPath As String
    Dim FindFirstFile As String
    Dim addInt as Integer

    ' ASSUMING YOU ALREADY HAVE THIS PATH
    ' WE WILL USE THIS AS BASE PATH
    ' AND WE WILL INCREMENT THE NUMBER AND YEAR IN IT
    oldPath = "G:\BUYING\Food Specials\2. Planning\1. Planning\1. Planner\8. 2017"

    ' EXTRACT 8. 2017 FROM oldPath
    tempPath = Mid(oldPath, InStrRev(oldPath, "\") + 1)

    ' GET THE YEAR DIFFERENCE
    addInt = Year(Date) - CInt(Split(tempPath, ".")(1))

    ' ADD THIS DIFFERENCE TO NUMBER AND YEAR
    ' AND NOW YOU HAVE CURRENT YEAR FOLDER
    newTemp = Split(tempPath, ".")(0) + addInt & ". " & Split(tempPath, ".")(1) + addInt

    FindFirstFile = Dir$(Path & "\" & "*.xlsx")

    While (FindFirstFile <> "")
        If InStr(FindFirstFile, "Test") > 0 Then
            FindDepotMemo = Path & FindFirstFile
            Exit Function
        End If
        FindFirstFile = Dir
    Wend

End Function