VBA比较两个文件夹以查找不同的文件

时间:2016-06-16 10:07:17

标签: vba directory createobject

我有2个文件夹 textFiles & excelFiles ,它们具有相同的文件名但扩展名不同(对于textFiles = .txt& excelFiles = .xlsx), 我编写了一个代码,以查找textFiles中的文件是否存在于excelFiles中以调用创建它的函数。

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

End Sub

但该领域" oFileExcel"在我的代码中没有返回字典而是字符串 帮助PLZ

1 个答案:

答案 0 :(得分:0)

您无法按照oFileExcel = exl.Name的方式分配值,您需要使用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"
...