2016-05-10 33 views
1

從幾天以來,我一直在搜索vba或vbs代碼以列出包含US或IS的所有文件夾在特定目錄中。 到目前爲止,我有下面的代碼列出所有的文件,但不是文件夾名稱。在包含文件夾名稱的目錄中查找文件夾作爲US-和IS- vba

'Force the explicit declaration of variables 
Option Explicit 
Sub Get_File_Name() 
Dim objFSO As Object 
Dim SearchString As String 
Dim TestPos As Integer 

Dim objFolder As Object 
Dim objSubFolder As Object 
Dim i As Integer 
'Create an instance of the FileSystemObject 
Set objFSO = CreateObject("Scripting.FileSystemObject") 
'Get the folder object 
Set objFolder = objFSO.GetFolder("C:\Working Documents\Client\OPTICS\") 
i = 1 
For Each objSubFolder In objFolder.SubFolders 
If InStr(1, objSubFolder.Name, "US-", vbTextCompare) > 0 Or InStr(1, objSubFolder.Name, "IS-", vbTextCompare) > 0 Then 
    'print folder name 
    Cells(i + 1, 1) = objSubFolder.Name 
    'print folder path 
    Cells(i + 1, 2) = objSubFolder.Path 
    i = i + 1 
Else 
End If 
Next objSubFolder 
End Sub 

任何幫助真的很感激。

回答

1

使用裏面的一個簡單InStr() For Each循環來檢查文件名中包含US-或IS-:

If InStr(1, objSubFolder.Name, "US-", vbTextCompare) > 0 Or InStr(1, objSubFolder.Name, "IS-", vbTextCompare) > 0 
+0

您好,感謝您的幫助......我不知道爲什麼代碼所賜我運行時錯誤。 –

+0

究竟是什麼給了你哪個錯誤?我拷貝你的代碼並運行它沒有任何問題 – gizlmo

+0

i = 1 For Each objSubFolder In objFolder.SubFolders If InStr(1,objSubFolder.Name,「US-」,vbTextCompare)> 0或者InStr(1,objSubFolder.Name, 「IS-」,vbTextCompare)> 0然後 '打印的文件夾名稱 細胞第(i + 1,1)= objSubFolder.Name ' 打印文件夾路徑 細胞第(i + 1,2)= objSubFolder.Path I = I + 1 Else End If –

相關問題