IF语句未正确评估优于2010 vba

时间:2015-06-23 14:38:30

标签: excel vba

我正在尝试在文件名中查找具有特定文本的文件,并仅在这些文件上运行宏。一切都很棒但是应该选择特定文件的IF语句没有正确评估。我可以看到存储在变量中的文件名,我甚至尝试将变量从变量转换为字符串 - 但IF语句似乎没有读取名称。它在文件夹中的所有文件上运行宏。

Function Recurse(sPath As String) As String

    Dim FSO As New FileSystemObject
    Dim myFolder As Folder
    Dim mySubFolder As Folder
    Dim myFile As Variant
    Dim file As String

    Set myFolder = FSO.GetFolder(sPath)

    For Each mySubFolder In myFolder.SubFolders
        For Each myFile In mySubFolder.Files

            file = myFile.Name

            If file <> "*CONSOLIDATED SUMMARY.xls*" Then
                Application.Workbooks.Open fileName:=myFile
                Call UnmergeAndResize
                ActiveWorkbook.Close SaveChanges:=True
            End If
        Next
        Recurse = Recurse(mySubFolder.path)
    Next

End Function

1 个答案:

答案 0 :(得分:1)

您不能使用*字符在VBA中执行部分字符串匹配。

相反,您应该将包含If语句的行替换为:

    If InStr(file, "CONSOLIDATED SUMMARY.xls") = 0 Then