mmichaelx
mmichaelx

Reputation: 95

EXCEL VBA: Access "Debug.Print"

I'm using a code that looks like the following to call the names from a folder:

Sub PrintFilesNames()
Dim file As String
file = Dir$(PathToFolder)
While (Len(file) > 0)
    Debug.Print file
    file = Dir
Wend
End Sub

It prints the names all to the immediate folder. Now is there a way I can use VBA to search through the files that have been printed, select a few containing a certain substring, and then paste them into an excel sheet?

Thank you!

Michael

Upvotes: 0

Views: 394

Answers (1)

Tim Williams
Tim Williams

Reputation: 166745

You can use a pattern in Dir() to do this:

Sub PrintFilesNames()
Dim file As String, c as range
    Set c = thisworkbook.sheets("Sheet1").Range("A1")
    file = Dir$(PathToFolder & "\*yoursubstring*.xls")
    While (Len(file) > 0)
        c.value = file
        Set c = c.offset(1,0)
        file = Dir
    Wend
End Sub

Upvotes: 2

Related Questions