I'm having issues with the wildcard *. I want to search for a document in a folder and return its location. The issue is that the document can be either .xlsx or .pdf; therefore, I wanted to add "*" to the name so that it would find either.
I have this:
Sub Test2()
Dim FSO As New FileSystemObject
Dim myFolder As Scripting.Folder
Dim mySubFolder As Scripting.Folder
Dim myFile As File
Set myFolder = FSO.GetFolder("Folder Path")
Set Team3 = ThisWorkbook.Worksheets("Team 3 & 3a")
Rng = Team3.Range("B4:B9")
For Each batch In Rng
Filename = batch & "*"
For Each mySubFolder In myFolder.SubFolders
For Each myFile In mySubFolder.Files
If myFile.Name = Filename Then
MsgBox myFile.Path
Exit For
End If
Next
Next
Next
End Sub
The issue I'm having is that, the wildcard * doesn't seem to be working and it seems to be interpreting it as text. If I replace the "*" with ".pdf" it does indeed find the files, but I needed it to search for any type of file.
I have removed the folder path from the above, but it is indeed searching in the correct folder. The code itself works, its just the "*" that doesn't seem to work.
Any help would be greatly appreciated.
Thank you, FunThomas for helping with this. Not sure how this site works, but the following was the answer given by him.
Sub Test2()
Dim FSO As New FileSystemObject
Dim myFolder As Scripting.Folder
Dim mySubFolder As Scripting.Folder
Dim myFile As File
Set myFolder = FSO.GetFolder("Folder Path")
Set Team3 = ThisWorkbook.Worksheets("Team 3 & 3a")
Rng = Team3.Range("B4:B9")
For Each batch In Rng
Filename = batch & "*"
For Each mySubFolder In myFolder.SubFolders
For Each myFile In mySubFolder.Files
If myFile.Name Like Filename Then
MsgBox myFile.Path
Exit For
End If
Next
Next
Next
End Sub