2013-12-09 86 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