Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

246
Views
Wildcard (*) not working. When searching a folder for a document with variable file type, the "*" function does not seem to be working

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.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!