0

I have a .xlsx file in the following directory:

G:\BUYING\Food Specials\2. Planning\1. Planning\1. Planner\8. 2017\2017 Planner.xlsx

I could just point it to this directory but the directory will change depending on the year it is.

So this directory could become:

G:\BUYING\Food Specials\2. Planning\1. Planning\1. Planner\9. 2018\2018 Planner.xlsx

To handle this i am trying to add wildcards to my path like so:

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

My workbook will not open. Please can someone show me where i am going wrong?

Sub:

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

Function:

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
CallumDA
  • 12,025
  • 6
  • 30
  • 52
user7415328
  • 1,053
  • 5
  • 24
  • 61

2 Answers2

0

Try this approach. The code loops through folder names 1. 2020, 1. 2019, 1. 2018 and current year, finding the lowest one, say, 1. 2018 exists. For that year it then looks for the highest month number, abandoning the previous for the next higher. If, say, 4. 2018 exists that is the path in which it looks for the file name using your original code.

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

If no 1. 2017 or higher is found, the function returns a null string. I couldn't test the code for lack of data.

Variatus
  • 14,293
  • 2
  • 14
  • 30
0

There are many ways but, I would use this approach:

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

As I've added comments to help you understand what am I doing.

ManishChristian
  • 3,759
  • 3
  • 22
  • 50