2016-06-16 114 views
0

我有2個文件夾TEXTFILES & excelFiles,它們具有相同的文件名,但具有不同的擴展名(TEXTFILES = .TXT & excelFiles = .xlsx)格式, 我寫代碼,以查找文本文件中的文件是否在excelFiles中不存在以調用創建它的函數。VBA比較兩個文件夾,找到文件型動物

Sub LookForNew() 
Dim dTxt As String 
Set fso = CreateObject("Scripting.FileSystemObject") 
Set filsTxt = fso.GetFolder("C:\txtFiles").Files 
Set filsExcel = fso.GetFolder("C:\excelFiles").Files 
Set oFileExcel = CreateObject("Scripting.Dictionary") 
Set tFileExl = CreateObject("Scripting.Dictionary") 
Set oFileExl = CreateObject("Scripting.Dictionary") 
For Each fil In filsTxt 
    dTxt = fil.Name 
    For Each exl In filsExcel 
     oFileExcel = exl.Name 
     oFileExl = Split(oFileExcel, ".") 
     tFileExl = oFileExl(0) 
     Next exl 
     If Not (tFileExl.Exists(dTxt)) Then 
      ' Call function 
     Else 
     MsgBox "No more files to convert" 
     End If 
Next fil 
Set fso = Nothing 

末次

但在我的代碼不會返回一個字典,而是一字符串 幫助PLZ

回答

0

您不能分配一個值,你做oFileExcel = exl.Name的方式現場「oFileExcel」,你需要使用oFileExcel.Add = exl.Name

看一看的dictionary的文檔:

Dim d     'Create a variable 
Set d = CreateObject(Scripting.Dictionary) 
d.Add "a", "Athens"  'Add some keys and items 
d.Add "b", "Belgrade" 
d.Add "c", "Cairo" 
...