【问题标题】:VBA Open workbook with wildcard?VBA用通配符打开工作簿?
【发布时间】:2017-04-06 13:58:46
【问题描述】:

我在以下目录中有一个 .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) & "\"

我的工作簿打不开。 请问有人可以告诉我哪里出错了吗?

子:

'Find Planner
If Len(FindDepotMemo) Then
Set wb2 = Workbooks.Open(FindDepotMemo, ReadOnly:=True, UpdateLinks:=False)
End If

功能:

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

【问题讨论】:

  • 这似乎是对您的问题的公平回答。 stackoverflow.com/questions/19527415/…
  • 我不明白您为什么不知道要打开的确切文件?您肯定会从用户那里获取输入,例如strYearOfInterest,然后使用该输入设置一个变量,然后将其传递到文件名的字符串中?
  • @Variatus 谢谢我试过了,但我认为这是因为我使用带数字的通配符并且它不一样
  • 您的最后一个文件夹应该是8. 2017 吗?你不能把它当作只有2017吗?因为,没有8.,一切正常。
  • @ManishChristian 不幸的是,我无法更改文件夹:(

标签: vba excel wildcard


【解决方案1】:

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

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

因为我添加了 cmets 来帮助您了解我在做什么。

【讨论】:

    【解决方案2】:

    试试这个方法。代码遍历文件夹名称 1. 2020、1. 2019、1. 2018 和当前年份,找到最低的文件夹名称,例如 1. 2018 存在。对于那一年,它会寻找最高的月份数,放弃前一个以获得下一个更高的月份数。例如,如果 4. 2018 存在,则它是使用原始代码查找文件名的路径。

    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
    

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

    【讨论】:

      猜你喜欢
      • 2013-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-13
      • 1970-01-01
      • 1970-01-01
      • 2017-01-16
      • 1970-01-01
      相关资源
      最近更新 更多