-1

I'm trying to find a file in subfolder. Found this code, and with the full filename it works, but now I want to find the file with a partial I know. Something like this:

 If myFile.Name = "*344148*" Then

But this is not working, below the complete code

Function Recurse(sPath As String) As String

    Dim FSO As New FileSystemObject
    Dim myFolder As Folder
    Dim mySubFolder As Folder
    Dim myFile As File

    Set myFolder = FSO.GetFolder(sPath)

    For Each mySubFolder In myFolder.SubFolders
        For Each myFile In mySubFolder.Files
        Debug.Print myFile.Name
        Debug.Print myFile
           ' If myFile.Name = "*" & Range("B2").Value & "*" Then
           ' If myFile = "*" & Range("B2").Value & "*" Then
            If myFile.Name = "*344148*" Then
                Debug.Print myFile.Name & " in " & myFile.Path 'Or do whatever you want with the file
                Workbooks.Open myFile.Path '& myFile.Name
                Exit For
            End If
        Next
        Recurse = Recurse(mySubFolder.Path)
    Next

End Function

Sub TestR()

    Call Recurse("P:\NetworkLocation\")

End Sub
Niles
  • 121
  • 1
  • 9
  • 4
    You want to use `Like`, not `=` - https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/like-operator – braX Feb 14 '23 at 08:02
  • Does this answer your question? [VBA: How to use like operator to a list of value?](https://stackoverflow.com/questions/55144780/vba-how-to-use-like-operator-to-a-list-of-value) – braX Feb 14 '23 at 08:03
  • https://devblogs.microsoft.com/oldnewthing/20111227-00/?p=8793 – GSerg Feb 14 '23 at 09:31

1 Answers1

1

You can replace If myFile.Name = "*344148*" Then with if instr(1,myFile.Name,"344148") <> 0 Then

Milad
  • 77
  • 11