2013-12-09 91 views
0

我使用看起來像下面從一個文件夾叫的名字代碼:EXCEL VBA:訪問「Debug.Print」

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

它打印的名字都以眼前的文件夾中。現在有什麼方法可以使用VBA搜索已打印的文件,選擇一些包含特定子字符串的文件,然後將它們粘貼到Excel表格中?

謝謝!

邁克爾

+0

有些已打印,有的還沒有,而您只需要前者。那是對的嗎? – Smandoli

回答

2

您可以使用迪爾模式()來做到這一點:

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 
+0

驚人的工作就像一個魅力!這個問題困擾了我好幾天,所以非常感謝Tim – mmichaelx